1

我正在尝试调用createTableIfNotExists这个 npm 包,并在服务器端的 Meteor 中同步调用。 https://www.npmjs.com/package/azure-storage

但是,回调签名是 typefunction(error, result, response)而不是传统的function(error,result).

1)因此,我不能使用Meteor.wrapAsync,而是必须使用Meteor.bindEnvironment

2)我称之为'bindEnvironment'如下。注意带有 3 个参数的回调。这可行,但现在我想将 , 提取return value回原始方法(即原始纤维)。

请注意,简单地在外部定义“addResult”createTableService是行不通的,因为内部的回调bindEnvironment相对于外部代码异步运行......即demoFunction()在回调设置之前返回addResult

var demoFunction = function(){
    var addResult = null;
    var tableService = azure.createTableService(acctName, acctKey);
                    tableService.createTableIfNotExists(asTableName, Meteor.bindEnvironment(function(error,result,response){
                        if (error) {
                            throw new Meteor.Error(500, "table create error - " + asTableName + ": "+ error);
                        }
                        console.log(result);
                        if(result){
                            addResult = /*call to meteor method*/ 
                            return addResult;
                        }                   
                    }));

    return addResult; //this is what I would like to do, but will always be null, as written.

}

我怎样才能调用createTableIfNotExists并仍然返回addResult 到调用的函数 demoFunction()

谢谢!

4

1 回答 1

0

您可以使用Future来自fibers/future( node-fibers ) 包的包,它只是一个不同的纤程抽象层,因为这个 async/await 实现这个 Promise 实现也是。(注意,不建议直接使用光纤)。

通用未来模式

正如您已经指出的,您也可以使用 async/await 或使用 Promise 来解决此问题。

但是,如果您想知道如何使用Future实例,可以遵循这个流行的模式,我在许多应用程序中都使用了它。

Meteor.bindEnvironment 的未来模式

包含bindEnvironment到这个模式中,并稍微重构一下代码,它看起来像下面这样:

import Future from 'fibers/future';

Meteor.methods({
    myMeteorMethod: function() {
        // load Future
        const myFuture = new Future();

        // create bound callback, that uses the future
        const boundCallback = Meteor.bindEnvironment(function (error,results){
            if(error){
              myFuture.throw(error);
            }else{
              myFuture.return(results);
            }
        });

        // call the function and pass bound callback
        SomeAsynchronousFunction("foo", boundCallback);

        return myFuture.wait();
    }
});

应用于代码示例

将模式的这种修改应用到您的代码中,可能会导致类似于下面的代码。请注意,我只是因为可读性而将回调与在函数内部创建的分开。您当然可以像通常那样在函数头中创建它。

import Future from 'fibers/future';

var demoFunction = function(){
    // load Future
    const myFuture = new Future();

    // create bound callback, that uses the future
    const boundCallback = Meteor.bindEnvironment(function (error, result, response) {
        if (error) {
            myFuture.throw(new Meteor.Error(500, "table create error - " + asTableName + ": "+ error));
        }

        // do whatever you need to do here
        // with result and response...

        if (result) {
            myFuture.return(result);
        }                   
    });

    var tableService = azure.createTableService(acctName, acctKey);

    // pass the bound callback here
    tableService.createTableIfNotExists(asTableName, boundCallback);

    return myFuture.wait();

}
于 2018-01-23T09:21:00.400 回答