【问题标题】:Future not working properly未来无法正常工作
【发布时间】:2019-01-14 07:27:46
【问题描述】:
const Future = require('fibers/future')
function myfunc() {
    var future = new Future();
    Eos().getInfo((err, res) => {
        future["return"]=res;
    })
    return future.wait();
};
console.log(myfunc());

错误是不能等待没有光纤请帮我解决这个问题

【问题讨论】:

  • 你能试试这个吗? var fn = async function() { Eos().getInfo((err, res) => { return res; }) }.future(); var 未来 = 等待 fn();未来.wait();
  • future.return 是一个函数,所以你应该调用它,而不是覆盖它 (future.return(res);)。在我看来,在最近的 JS 版本中,promises 和 async/await 更容易使用。

标签: javascript node.js meteor fibers


【解决方案1】:

由于错误指出,future 只能“等待”在光纤中运行

console.log(Fiber(myfunc).run());

【讨论】:

  • 它给了我未定义
  • @yashvadhvani 因为您的原始函数没有返回您需要的任何值 future.return("some value")
【解决方案2】:

用承诺摆脱这个。

function myfunc() {
    return new Promise((resolve, reject) => {
        Eos().getInfo((err, res) => {
            if (err) {
                reject(err);
            }
            else {
                resolve(res);
            }
        });
    });
}
myfunc()
    .then((res) => {
        console.log(res);
    })
    .catch(err => {
        console.log(err);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2018-11-26
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-09-01
    相关资源
    最近更新 更多