0

I found one article that kind of explains how to queue up asynchronous operations, but it's rather vague in that it says if you return a "promise-like" object from a then function, it will make the next then wait for it to complete. I don't know what "promise-like" means, so I took a stab at this (see code below), and it works, but I'm not sure if this is the correct way to go about doing this or not.

let promise = new Promise(function (resolve, reject) {
  someAsyncOperation(function () {
    resolve({done: true});
  });
});

promise.then(function (val) {
  return new Promise(function (resolve, reject) {
    anotherAsyncOperation(function () {
      resolve({doneAgain: true});
    });
  });
}).then(function (val) {
  // This only occurs after the anotherAsyncOperation is done
});
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
ffxsam
  • 26,428
  • 32
  • 94
  • 144
  • What your asking, and what your code "does" isn't clear, but if the `*AsyncOperation` functions are callback style, then it is in the right direction, but better be "promisified". – Amit Sep 17 '15 at 22:26
  • "but better be 'promisified'".. can you elaborate on that? – ffxsam Sep 17 '15 at 22:29
  • The term *promisification* refers to the act of wrapping a callback style function or a set of callback style functions with a `Promise` object. That's roughly what your code does, but there are libraries that do that more cleanly and take better care of exception & error handling. Google it up. – Amit Sep 17 '15 at 22:31
  • If you want to know what "promise-like" means here, have a look at [what is the difference between the terms “thenable” and “promise”?](http://stackoverflow.com/q/29435262/1048572) But yes, returning a "real" `Promise` is just fine (the correct™ way) for this - in fact [this pattern is what make promises so powerful](http://stackoverflow.com/a/22562045/1048572). – Bergi Sep 18 '15 at 16:07

1 Answers1

1

Yes, your code should function to create a promise that waits until anotherAsyncOperation is done.

let promise = new Promise(function(resolve, reject) {
    console.log('1');
    someAsyncOperation(function() {
        resolve({ done: true });
    });
}).then(function(val) {
    // val == { done: true }
    return new Promise(function(resolve, reject) {
        console.log('2');
        anotherAsyncOperation(function() {
            resolve({ doneAgain: true });
        });
    });
}).then(function(val) {
    // val == { doneAgain: true }
    console.log('3');
});

What @Amit means is that you can create "Promisified" versions of a callback-style async operation.

function promisify( operation ) {
    return function( ) {
        return new Promise(function(resolve, reject) {
            operation(function( err, val ) {
                if (err) reject(err);
                else resolve(val);
            });
        });
    };
}

var someAsyncOp = promisify( someAsyncOperation );
var anotherAsyncOp = promisify( anotherAsyncOperation );

console.log('1');
var promise = someAsyncOp()
    .then(function( val ) {
        console.log('2');
        return anotherAsyncOp();
    })
    .then(function( val ) {
        console.log('3');
    });

Note, there are promise libraries with much better promisify functions than that one I showed. Please use one of those.

TbWill4321
  • 8,626
  • 3
  • 27
  • 25