简体   繁体   English

使用promise编写可读代码的最佳方法是什么?

[英]What is the best way to write readable code using promises?

I have some node.js promise code that looks something like this: 我有一些node.js承诺代码,看起来像这样:

function myFunc(data) {
  Q(data).then(function(data) {
    return getPromise1(globalVar).then(function(res1a) {
      return getPromise2(JSON.parse(param2)).then(function(res2a) {
        return doStuff();
      }).then(function(res2b) {
        return getPromise3(data).then(function(res3a) {
          return getPromise4.then(function(res4a) {
            // more stuff
          })
        })
      })
    })
  })
})

As you can see, this code is not very readable. 如您所见,此代码不是很可读。 Is there a better way to do this? 有一个更好的方法吗?

If you don't need all of the results at once, just stop treating promises like callbacks: 如果您不需要一次所有的结果,那就停止像回调一样处理promise:

function myFunc(data) {
  Q(data).then(function(data) {
    return getPromise1(globalVar);
  }).then(function(res1a) {
    return getPromise2(JSON.parse(param2));
  }).then(function(res2a) {
    return doStuff();
  }).then(function(res2b) {
    return getPromise3(data);
  }).then(function(res3a) {
    return getPromise4;
  }).then(function(res4a) {
    // more stuff
  })
})

If you do, then you can try coroutines given generator function support (Q probably has something for that, but here's a Bluebird way): 如果这样做,那么您可以尝试在给定生成器函数支持的情况下进行协程 (Q可能为此提供支持,但这是一种蓝鸟方式):

var myFunc = bluebird.coroutine(function* myFunc(data) {
  var res1a = yield getPromise1(globalVar);
  var res2a = yield getPromise2(JSON.parse(param2));
  var res2b = yield doStuff();
  var res3a = yield getPromise3(data);
  var res4a = yield getPromise4;

  // more stuff
})

or synchronous inspection : 同步检查

function myFunc(data) {
  var res1a = getPromise1(globalVar);
  var res2a = res1a.then(function() {
    yield getPromise2(JSON.parse(param2));
  });
  var res2b = res2a.then(function() {
    // feel free to use res1a.value() here;
    // you know that it has to have been resolved
    doStuff();
  });

  // …

  return res4a;
}

In your example it would help to use lambda expressions: 在您的示例中,使用lambda表达式将有所帮助:

Q(data) .then(data => getPromise1(globalVar) .then(re1a => getPromise2(JSON.parse(param2)

and so on. 等等。 Without nesting and in this style, it looks much less like callback hell :) 没有嵌套并且以这种样式,它看起来更像是回调地狱:)

One of the goals of promises besides separating data parameters from control flow parameters was actually to solve this issue of huge triangular blocks of code. 除了将数据参数与控制流参数分开之外,promise的目标之一实际上是解决巨大的三角形代码块的问题。

function myFunc(data) {
  Q(data).then(function(data) {
    return getPromise1(globalVar);
  }).then(function(res1a) {
    return getPromise2(JSON.parse(param2));
  }).then(function(res2a) {
    return doStuff();
  }).then(function(res2b) {
    return getPromise3(data);
  }).then(function(res3a) {
    return getPromise4;
  }).then(function(res4a) {
    // more stuff
  })
}

Now, the only reason you would ever need to nest a promise is if you need to use data returned from a promise in a function not immediately following it. 现在,您唯一需要嵌套一个Promise的原因是,如果您需要在一个不立即跟在Promise之后的函数中使用从Promise返回的数据。 See below: 见下文:

doAsyncA().then(function(x) {
  doAsyncB().then(function(y) {
    doSyncUsingBothReturns(x, y);
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM