最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - chain promises in javascript - Stack Overflow

programmeradmin2浏览0评论

I've created many promises like that, in order to create object in my database.

var createUserPromise = new Promise(
  function(resolve, reject) {
    User.create({
      email: '[email protected]'
    }, function() {
      console.log("User populated"); // callback called when user is created
      resolve();
    });
  }
); 

At the end, I want call all my promises in the order I want. (because somes object depend of other, so I need to keep that order)

createUserPromise
  .then(createCommentPromise
    .then(createGamePromise
      .then(createRoomPromise)));

So I expect to see :

User populated
Comment populated
Game populated
Room populated

Unfortunately, this messages are shuffled and I don't understand what.

Thanks

I've created many promises like that, in order to create object in my database.

var createUserPromise = new Promise(
  function(resolve, reject) {
    User.create({
      email: '[email protected]'
    }, function() {
      console.log("User populated"); // callback called when user is created
      resolve();
    });
  }
); 

At the end, I want call all my promises in the order I want. (because somes object depend of other, so I need to keep that order)

createUserPromise
  .then(createCommentPromise
    .then(createGamePromise
      .then(createRoomPromise)));

So I expect to see :

User populated
Comment populated
Game populated
Room populated

Unfortunately, this messages are shuffled and I don't understand what.

Thanks

Share Improve this question edited Jan 9, 2016 at 17:43 ttncrch asked Jan 9, 2016 at 17:34 ttncrchttncrch 7,25311 gold badges38 silver badges62 bronze badges 1
  • Note - mongoose already returns promises - your code should have new Promise exactly zero times. Please see stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it and mongoosejs.com/docs/promises.html – Benjamin Gruenbaum Commented Jan 9, 2016 at 18:43
Add a comment  | 

2 Answers 2

Reset to default 16

Looks like you understood promises wrong, re-read some tutorials on promises and this article.

As soon as you create a promise using new Promise(executor), it is invoked right away, so all your function actually are executed as you create them and not when you chain them.

createUser actually should be a function returning a promise and not a promise itself. createComment, createGame, createRoom too.

Then you will be able to chain them like this:

createUser()
.then(createComment)
.then(createGame)
.then(createRoom)

Latest versions of mongoose return promises if you don't pass callbacks, so you don't need to wrap it into a function returning a promise.

You should wrap your Promises into functions. The way you're doing, they are called right away.

var createUserPromise = function() {
  return new Promise(
    function(resolve, reject) {
      User.create({
        email: '[email protected]'
      }, function() {
        console.log("User populated"); // callback called when user is    created
        resolve();
      });
    }
  );
};

Now you can chain Promises, like this:

createUserPromise()
.then(createCommentPromise)
.then(createGamePromise)
.then(createRoomPromise);
发布评论

评论列表(0)

  1. 暂无评论