Javascript Meteor:如何使用2个参数调用函数并等待结果

Javascript Meteor:如何使用2个参数调用函数并等待结果,javascript,asynchronous,meteor,async-await,Javascript,Asynchronous,Meteor,Async Await,在过去的几天里,我一直在读关于和的书,但我仍然不能完全理解它。我很难找到一个简单的例子来说明我如何制作这样的东西: Meteor.methods({ theMethod: function(){ var numberOne = 4; var numberTwo = 5; var result = calculateNumber(numberOne, numberTwo); console.log('This is the result: ', result);

在过去的几天里,我一直在读关于和的书,但我仍然不能完全理解它。我很难找到一个简单的例子来说明我如何制作这样的东西:

Meteor.methods({
  theMethod: function(){
    var numberOne = 4;
    var numberTwo = 5;
    var result = calculateNumber(numberOne, numberTwo);
    console.log('This is the result: ', result);
  }
});

calculateNumber = function(numberOne, numberTwo){
  var result = numberOne + number Two
  //but lets assume an ultra complex calculation that takes a while
  return result;
}
如何使console.log在没有回调的情况下等待返回结果,而不使用wait/wrapAsync语法

提前感谢!:)

编辑:

以下是我尝试的代码:

Meteor.methods({
  theMethod: async function(){
    const no1 = 4, no2 = 5;
    //calculateNumber should return a promise, if it doesnt, Promise.resolve makes the returned value a promise
    const calculateNumberPromise = Promise.resolve(calculateNumber(no1, no2));
    const res = await calculateNumberPromise;
    console.log('Number calculated', res);
    //the number can be returned directly to the client:
    return res
  }
});

calculateNumber = function(no1, no2){
  function sleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
  }
  sleep(5000).then(() => {
    return no1 + no2;
  });
}

有没有一种方法可以让所有的事情都在计算之后等待?如果不可能,我必须在何处/如何嵌套console.log以使其等待?

在Meteor中使异步函数同步的最简单方法是使用

例如:

Meteor.methods({
  theMethod() {
    const numberOne = 4;
    const numberTwo = 5;
    const result = Meteor.wrapAsync(asyncFun(numberOne, numberTwo));
    console.log('This is the result: ', result);
  }
});

您的
calculateNumber
函数是同步的,正如您在问题中列出的那样,
wrapAsync
旨在包装一个真正的异步函数。请注意,长时间运行!==异步的。还请注意,正在包装的异步函数必须返回错误和结果,而不仅仅是结果。当然,当您使用
wrapAsync
时,只会将结果返回给调用代码,因此您需要使用
try/catch
块来捕获任何错误。

在Meteor中使异步函数同步的最简单方法是使用

例如:

Meteor.methods({
  theMethod() {
    const numberOne = 4;
    const numberTwo = 5;
    const result = Meteor.wrapAsync(asyncFun(numberOne, numberTwo));
    console.log('This is the result: ', result);
  }
});

您的
calculateNumber
函数是同步的,正如您在问题中列出的那样,
wrapAsync
旨在包装一个真正的异步函数。请注意,长时间运行!==异步的。还请注意,正在包装的异步函数必须返回错误和结果,而不仅仅是结果。当然,当您使用
wrapAsync
时,只会将结果返回给调用代码,因此您需要使用
try/catch
块来捕获任何错误。

使用独占
Promise
(即no
async
/
wait
),您可以执行以下操作:

Meteor.methods({
  theMethod: () => {
    const no1 = 4, no2 = 5;
    //calculateNumber should return a promise, if it doesnt, Promise.resolve makes the returned value a promise (unless calculateNumber is a sync function ie takes a callback)
    const calculateNumberPromise = Promise.resolve(calculateNumber(no1, no2));
    //If you want to log the output regardless:
    calculateNumberPromise
    .then(res => console.log('Number calculated', res))
    .catch(err => console.log('Error calcualting number', err))
    //The promise is resolved before the data is sent over the wire.
    return calculateNumberPromise;
  }
});
在客户机上,可以调用此方法并使用承诺。理想情况下,您现在应该使用。与经过验证的方法结合使用,可以使用。链接示例。更多可以在流星指南中找到

调整前面的代码段以使用
async
很简单:

Meteor.methods({
  theMethod: async function() {
    const no1 = 4, no2 = 5;
    //calculateNumber should return a promise, if it doesnt, Promise.resolve makes the returned value a promise
    const calculateNumberPromise = Promise.resolve(calculateNumber(no1, no2));
    const res = await calculateNumberPromise;
    console.log('Number calculated', res);
    //the number can be returned directly to the client:
    return res;
  }
});
注意在函数定义中添加了
async
。还可以找到一篇优秀的文章-查看Rob Fallows的其他文章

wrapAsync
很简单,但我建议坚持
Promise
async
/
wait
。如果您真的想使用它,可以在上找到一些旧的(但很好的)示例


如果有任何不清楚的地方,请要求澄清。

通过独家
承诺
(即无
异步
/
等待
),您可以执行以下操作:

Meteor.methods({
  theMethod: () => {
    const no1 = 4, no2 = 5;
    //calculateNumber should return a promise, if it doesnt, Promise.resolve makes the returned value a promise (unless calculateNumber is a sync function ie takes a callback)
    const calculateNumberPromise = Promise.resolve(calculateNumber(no1, no2));
    //If you want to log the output regardless:
    calculateNumberPromise
    .then(res => console.log('Number calculated', res))
    .catch(err => console.log('Error calcualting number', err))
    //The promise is resolved before the data is sent over the wire.
    return calculateNumberPromise;
  }
});
在客户机上,可以调用此方法并使用承诺。理想情况下,您现在应该使用。与经过验证的方法结合使用,可以使用。链接示例。更多可以在流星指南中找到

调整前面的代码段以使用
async
很简单:

Meteor.methods({
  theMethod: async function() {
    const no1 = 4, no2 = 5;
    //calculateNumber should return a promise, if it doesnt, Promise.resolve makes the returned value a promise
    const calculateNumberPromise = Promise.resolve(calculateNumber(no1, no2));
    const res = await calculateNumberPromise;
    console.log('Number calculated', res);
    //the number can be returned directly to the client:
    return res;
  }
});
注意在函数定义中添加了
async
。还可以找到一篇优秀的文章-查看Rob Fallows的其他文章

wrapAsync
很简单,但我建议坚持
Promise
async
/
wait
。如果您真的想使用它,可以在上找到一些旧的(但很好的)示例


如果其中有任何不清楚的地方,请要求澄清。

您发布的代码应按预期工作,控制台日志应输出CalculateEnumber的结果。我的印象是CalculateEnumber是一个基于示例的同步函数。该函数不是同步的。这是在问题中指定的。@如果CalculateEnumber函数花费很长时间,DerrickGre000000控制台日志将返回undefined,我可以在其中添加一些wait,但只需编写
var result=wait CalculateEnumber(numberOne,numberTwo)还不够。@sebmoris,你知道如何使它同步吗?:)我在写一个答案:d你发布的代码应该可以正常工作,控制台日志应该输出CalculateEnumber的结果。我的印象是CalculateEnumber是一个基于示例的同步函数。该函数不是同步的。这是在问题中指定的。@如果CalculateEnumber函数花费很长时间,DerrickGre000000控制台日志将返回undefined,我可以在其中添加一些wait,但只需编写
var result=wait CalculateEnumber(numberOne,numberTwo)还不够。@sebmoris,你知道如何使它同步吗?:)我在写一个答案:你能给我解释一下,为什么CalculateEnumber是同步的吗?我想如果它需要很长时间才能执行,那么它就不会阻塞。我尝试将计算速度降低5秒,然后打印时结果未定义..异步函数有回调<代码>计算枚举数
不允许。既然你在服务器上做了这一切,你也需要考虑一下,你能解释一下为什么计算数是同步的吗?我想如果它需要很长时间才能执行,那么它就不会阻塞。我尝试将计算速度降低5秒,然后打印时结果未定义..异步函数有回调<代码>计算枚举数
不允许。既然你在服务器上做了这一切,你也需要考虑我现在使用了这个函数“睡眠”(time){返回新的承诺((决心)= > SETTIMEOUT(解析,时间));}休眠(3000)。然后(()= {{)减慢运算数的执行。然后以上两个版本都没有帮助和<代码>控制台。日志(‘计算数’,RES)。printed with res=undefined and later Calculate Enumber printed现在已经完成了。我如何解决这个问题?您可以编辑您的an吗