[英]Meteor synchronous function call on the client
I'm using Meteor and React, and I'm trying return data from a third party javascript function 我正在使用Meteor和React,并且正在尝试从第三方javascript函数返回数据
The function takes a callback and returns a value. 该函数接受一个回调并返回一个值。 The function takes a few seconds to resolve
该功能需要几秒钟才能解决
At the moment the callback returns an undefined value but after a few seconds a console log will display the correct data 此刻回调函数返回一个未定义的值,但几秒钟后,控制台日志将显示正确的数据
My current code is as follows 我当前的代码如下
// ON THE CLIENT
// callback function that returns a deviceId
const callback = function( data ) {
// this callback fires with the correct data after a couple of seconds
console.log("Callback ", data);
return data;
};
// async function that calls third party function with callback
async function getDeviceId() {
const res = await captureDeviceId(callback);
return await res;
}
let response = getDeviceId().then( function(value) {
return value;
});
console.log("res", response);
The response returns a promise but does not resolve 响应返回一个承诺,但没有解决
Return {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
How can I wait for the functions response before returning the value? 在返回值之前,如何等待函数响应? I've tried Meteor wrapAsync, async-await and Promises without success.
我尝试过流星wrapAsync,异步等待和Promises都没有成功。
Any help would be appreciated 任何帮助,将不胜感激
Cheers, 干杯,
getDeviceId returns here a promise (even if you do a return inside the .then callback). getDeviceId在此处返回一个Promise (即使您在.then回调内部进行了返回)。 If you want to ge the value, do it like below:
如果您想要ge值,请按照以下步骤操作:
var response;
getDeviceId().then( function(value) {
response = value;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.