Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在返回值之前等待异步回调_Javascript_Node.js_Meteor_Coffeescript - Fatal编程技术网

Javascript 在返回值之前等待异步回调

Javascript 在返回值之前等待异步回调,javascript,node.js,meteor,coffeescript,Javascript,Node.js,Meteor,Coffeescript,与 菲拉 文件 如何获取console.log userID('someUsername')以同时输出ID,而不是0?也就是说,让它在返回id之前等待 我尝试过用Meteor.wrapAsync和Meteor.bindEnvironment随机包装东西,但似乎没有成功。您可以在回调中完成工作,也可以使用承诺或事件发射器控制流: "use strict"; var Q = require('q'); var EventEmitter = require('events').EventEmitte

菲拉

文件

如何获取
console.log userID('someUsername')
以同时输出ID,而不是0?也就是说,让它在返回id之前等待


我尝试过用Meteor.wrapAsync和Meteor.bindEnvironment随机包装东西,但似乎没有成功。

您可以在回调中完成工作,也可以使用承诺或事件发射器控制流:

"use strict";

var Q = require('q');
var EventEmitter = require('events').EventEmitter;

// using a promise
var defer = Q.defer();

// using an emitter
var getUserID = new EventEmitter();

var id = 0;
getUser("somename", function (err, data, res) {
    if ( data )
        id = data.id;
    // simply do the work in the callback
    console.log("In callback: "+data.id);
    // this signals the 'then' success callback to execute
    defer.resolve(id);
    // this signals the .on('gotid' callback to execute
    getUserID.emit('gotid', id);
});

console.log("oops, this is async...: "+id);

defer.promise.then(
    function (id) {
        console.log("Through Promise: "+id);
    }
);

getUserID.on('gotid',
             function (id) {
                 console.log("Through emitter: "+id);
             }
            );

function getUser (username, callback) {
    setTimeout( function () {
        callback(null, { id : 1234 }, null);
    }, 100);
}

谢谢大家。我找到了一个解决方案


把它放在对
someAPI.getUser
的回调中。欢迎来到奇妙的异步世界,你不能这样做。有关流星的答案是使用期货。读和读。
getUser: (username, callback) ->
  return api.get 'users/show', { 'username': username }, callback
"use strict";

var Q = require('q');
var EventEmitter = require('events').EventEmitter;

// using a promise
var defer = Q.defer();

// using an emitter
var getUserID = new EventEmitter();

var id = 0;
getUser("somename", function (err, data, res) {
    if ( data )
        id = data.id;
    // simply do the work in the callback
    console.log("In callback: "+data.id);
    // this signals the 'then' success callback to execute
    defer.resolve(id);
    // this signals the .on('gotid' callback to execute
    getUserID.emit('gotid', id);
});

console.log("oops, this is async...: "+id);

defer.promise.then(
    function (id) {
        console.log("Through Promise: "+id);
    }
);

getUserID.on('gotid',
             function (id) {
                 console.log("Through emitter: "+id);
             }
            );

function getUser (username, callback) {
    setTimeout( function () {
        callback(null, { id : 1234 }, null);
    }, 100);
}
getUserID = Async.wrap((username, callback) ->
  someAPI.getUser username, (err, data, res) ->
    callback err, data.id
)

console.log getUserID('someUsername')