Results 1 to 2 of 2

Thread: question about example of promise design pattern

Hybrid View

  1. #1
    Join Date
    Feb 2011

    Question question about example of promise design pattern

    I am reading this article.

    Now he give the following example:
    // take a callback function and change it to return a promise
    Aplus.toPromise = function(fn) {
     return function() {
      // promise to return
      var promise = Aplus();
      //on error we want to reject the promise
      var errorFn = function(data) {
      // fulfill on success
      var successFn = function(data) {
      // run original function with the error and success functions
      // that will set the promise state when done
       [errorFn, successFn].concat([].slice.call(arguments, 0)));
      return promise;
    What I don't understand is in line 20, the line:
    [errorFn, successFn].concat([].slice.call(arguments, 0))
    What is the reason to use slice if he any way take all the array.
    As far as I understand it, he could write it like this:
    [errorFn, successFn].concat(arguments)

    So in short, my question is why the use of slice there if in any case you take all the array?

  2. #2
    Join Date
    Sep 2013
    What slice() does in this case is create an empty array, then iterate through the object it's running on and keep appending the elements of that object to the empty array it created, which is eventually returned.

    [].slice returns a function object. A function object has a function call() which calls the function assigning the first parameter of the call() to this; in other words, making the function think that it's being called from the parameter rather than from an array.

    So i think it better to use slice because your caching the prototype lookup should be fastest.

    Hope this helps.
    strad solutionswww.stradsolutions.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center