第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使函數(shù)等到使用node.js調(diào)用回調(diào)

如何使函數(shù)等到使用node.js調(diào)用回調(diào)

GCT1015 2019-07-29 15:27:19
如何使函數(shù)等到使用node.js調(diào)用回調(diào)我有一個(gè)簡(jiǎn)化的功能,如下所示:function(query) {   myApi.exec('SomeCommand', function(response) {     return response;   });}基本上我希望它調(diào)用myApi.exec,并返回回調(diào)lambda中給出的響應(yīng)。但是,上面的代碼不起作用,只是立即返回。只是為了一個(gè)非常hackish嘗試,我嘗試了下面沒(méi)有工作,但至少你明白了我想要實(shí)現(xiàn)的目標(biāo):function(query) {   var r;   myApi.exec('SomeCommand', function(response) {     r = response;   });   while (!r) {}   return r;}基本上,什么是一個(gè)好的'node.js /事件驅(qū)動(dòng)'的方式來(lái)解決這個(gè)問(wèn)題?我希望我的函數(shù)等到調(diào)用回調(diào),然后返回傳遞給它的值。
查看完整描述

3 回答

?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

“good node.js / event driven”這樣做的方法就是不要等待。

與使用像節(jié)點(diǎn)這樣的事件驅(qū)動(dòng)系統(tǒng)幾乎所有其他內(nèi)容一樣,您的函數(shù)應(yīng)該接受一個(gè)回調(diào)參數(shù),該參數(shù)將在計(jì)算完成時(shí)調(diào)用。調(diào)用者不應(yīng)該等待正常意義上的“返回”值,而是發(fā)送將處理結(jié)果值的例程:

function(query, callback) {
  myApi.exec('SomeCommand', function(response) {
    // other stuff here...
    // bla bla..
    callback(response); // this will "return" your value to the original caller
  });}

所以你不要這樣使用它:

var returnValue = myFunction(query);

但是像這樣:

myFunction(query, function(returnValue) {
  // use the return value here instead of like a regular (non-evented) return value});


查看完整回答
反對(duì) 回復(fù) 2019-07-29
?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

實(shí)現(xiàn)此目的的一種方法是將API調(diào)用包裝到promise中,然后使用await等待結(jié)果。

// let's say this is the API function with two callbacks,// one for success and the other for errorfunction apiFunction(query, successCallback, errorCallback) {
    if (query == "bad query") {
        errorCallback("problem with the query");
    }
    successCallback("Your query was <" + query + ">");}// myFunction wraps the above API call into a Promise// and handles the callbacks with resolve and rejectfunction apiFunctionWrapper(query) {
    return new Promise((resolve, reject) => {
        apiFunction(query,(successResponse) => {
            resolve(successResponse);
        }, (errorResponse) => {
            reject(errorResponse)
        });
    });}// now you can use await to get the result from the wrapped api function// and you can use standard try-catch to handle the errorsasync function businessLogic() {
    try {
        const result = await apiFunctionWrapper("query all users");
        console.log(result);

        // the next line will fail
        const result2 = await apiFunctionWrapper("bad query");
    } catch(error) {
        console.error("ERROR:" + error);
    }}// call the main functionbusinessLogic();

輸出:

Your query was <query all users>ERROR:problem with the query


查看完整回答
反對(duì) 回復(fù) 2019-07-29
  • 3 回答
  • 0 關(guān)注
  • 530 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)