在不使用延迟 API 的情况下,如何从不允许返回值的函数返回承诺? [英] How do you return promises from functions that don't allow for return values without using deferred APIs?

查看:32
本文介绍了在不使用延迟 API 的情况下,如何从不允许返回值的函数返回承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问是否在承诺中包装承诺一个反模式?几天前这导致我这样做.

I asked Is wrapping a promise in a promise an anti-pattern? a few days ago which led me to this.

在那种情况下,我不得不处理一个 setTimeout,它不允许返回值.幸运的是,您可以使用内置的 delay 函数来处理这个问题(至少对于 bluebird 承诺).

In that case I had to deal with a setTimeout, which doesn't allow for return values. Thankfully you can deal with this by using the built in delay function (at least for bluebird promises).

但是对于像 gulp 任务这样的事情呢?也受到另一个问题的启发:如何将对象从 gulpfile 传递到 nodejs 中的另一个 JavaScript 文件?

But what about for something like a gulp task? Also inspired by another question: How to pass an object from gulpfile to another JavaScript file in nodejs?

var stuff;
gulp.task('LoadYamlFiles', function() {
  // do stuff
  stuff = 'blah';
});

module.exports = { stuff };

导出不会是废话",因为 gulp 任务是异步运行的.这可以使用 Promise 来解决.

The export wouldn't be 'blah' because the gulp task runs asynchronously. This can be solved using promises.

一种方法是使用延迟(来自 https 的代码://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns):

One way you could do it is using a deferred (code from https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns):

// setTimeout that returns a promise
function delay(ms) {
    var deferred = Promise.pending();
    setTimeout(function(){
        deferred.resolve();
    }, ms);
    return deferred.promise;
}

根据他们的 wiki,这实际上不被视为反模式,但我看到 deferred 的使用非常不鼓励.承认这一点似乎很重要,因为 bluebird 甚至在他们的 API 中都没有 .pending() 方法,这意味着即使我想这样做,我也无法真正做到这一点.

And this actually isn't considered an anti-pattern according to their wiki, but I see the usage of deferred discouraged a lot. This seems important to acknowledge because bluebird doesn't even have the .pending() method in their API anymore, meaning I can't really do it this way even if I wanted to.

但是你不能这样做:

var stuff;
var myPromise = gulp.task('LoadYamlFiles', function() {
  return new Promise(function(resolve, reject) {
    // do stuff
    stuff = 'blah';
    resolve(stuff);
  })
});

module.exports = { myPromise };

因为匿名函数的返回值不是 myPromise 将包含的.它还会导致您可以在此处看到的问题 gulp 任务是否必须返回任何东西?

because the return value of the anonymous function isn't what myPromise will contain. Also it would cause the problems you can see here Does a gulp task have to return anything?

那么您如何处理这些通常会使用事实上已弃用的 deferred 模式的情况?

So how do you deal with these cases where normally you would use the de facto deprecated deferred pattern?

推荐答案

您将变量命名为 myPromise,这是第一个暗示 =,应该是承诺.那么让我们开始

You named your variable myPromise, that's a first hint that whatever is on the other side of the =, it should be a promise. So let's start with

var myPromise = new Promise((resolve, reject) => {
  // todo
});

module.exports { myPromise };

现在,让我们填写详细信息.

Now, let's fill in the details.

var myPromise = new Promise((resolve, reject) => {
  gulp.task('LoadYamlFiles', function() {
    let stuff = 'blah';
    resolve(stuff);
  });
});

一点重构给了我们

let myPromise = new Promise(resolve => 
  gulp.task('LoadYamlFiles', () => resolve('blah')));

我确定这不是一个真正的用例,但它仍然可以应用.

I'm sure this isn't a real usecase, but it can apply nonetheless.

另见:

这篇关于在不使用延迟 API 的情况下,如何从不允许返回值的函数返回承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆