NodeJS多功能承诺

时间:2016-02-23 16:34:52

标签: javascript node.js promise

我们说我有一些看起来像这样的代码:

var doSomething = function(parameter){
    //send some data to the other function
    return when.promise(function(resolveCallback, rejectCallback) {
        var other = doAnotherThing(parameter);
        //how do I check and make sure that other has resolved
        //go out and get more information after the above resolves and display

    }); 
};

var doAnotherThing = function(paramers){
    return when.promise(function(resolveCallback, rejectCallback) {
        //go to a url and grab some data, then resolve it
        var s = "some data I got from the url";
        resolveCallback({
            data: s
        });
    });
};

在完成和解析第一个var other功能之前,如何确保doSomething()已完全解决?我仍然围绕节点异步特征

我真的不知道怎么解释这个,所以我希望这是有道理的!非常感谢任何帮助

编辑:在此示例中,我将从外部资源中删除内容,然后在完成后,外出资源并获取新的项目列表。

更新代码

var doSomething = function(parameter){
        //send some data to the other function
        doAnotherThing(parameter).then(function(){
            //now we can go out and retrieve the information
        });
    };

var doAnotherThing = function(paramers){
    return when.promise(function(resolveCallback, rejectCallback) {
        //go to a url and grab some data, then resolve it
        var s = "some data I got from the url";
        resolveCallback({
            data: s
        });
    });
};

3 个答案:

答案 0 :(得分:3)

doAnotherThing的回归似乎是一种承诺。您只需链接then并将回调用于other即可。 then也已经返回了一个承诺。你可以改回来。

// Do stuff
function doSomething(){
  return doAnotherThing(parameter).then(function(other){
    // Do more stuff
    return other
  });
}

// Usage
doSomething().then(function(other){
  // other
});

答案 1 :(得分:0)

以下是如何完成您bluebird尝试的操作。

您可以在任何函数中使用Promise.resolve()Promise.reject()来返回Promise中的数据,该Promise可以直接在您的promise链中使用。基本上,通过使用这些方法返回包装结果数据,您可以在Promise链中使任何函数可用。

var Promise = require('bluebird');

var doSomething = function(parameter) {
  // Call our Promise returning function
  return doAnotherThing()
    .then(function(value) {
      // Handle value returned by a successful doAnotherThing call
    })
    .catch(function(err) {
      // if doAnotherThing() had a Promise.reject() in it
      // then you would handle whatever is returned by it here
    });
}

function doAnotherThing(parameter) {
  var s = 'some data I got from the url';

  // Return s wrapped in a Promise
  return Promise.resolve(s);
}

答案 2 :(得分:-1)

您可以使用async模块及其瀑布方法将函数链接在一起:

var async = require('async');

async.waterfall([
  function(parameter, callback) {
    doSomething(parameter, function(err, other) {
      if (err) throw err;
      callback(null, other); // callback with null error and `other` object
    });
  },
  function(other, callback) { // pass `other` into next function in chain
    doAnotherThing(other, function(err, result) {
      if (err) throw err;
      callback(null, result);
    })
  }
], function(err, result) {
  if (err) return next(err);
  res.send(result); // send the result when the chain completes
});

在我看来,让你更容易围绕一系列承诺。 See the documentation for explanation