简体   繁体   中英

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

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.

Any help would be appreciated

Cheers,

getDeviceId returns here a promise (even if you do a return inside the .then callback). If you want to ge the value, do it like below:

var response;
getDeviceId().then( function(value) { 
    response =  value;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM