Javascript 父函数内的异步进程-返回异步进程结果?

Javascript 父函数内的异步进程-返回异步进程结果?,javascript,asynchronous,promise,Javascript,Asynchronous,Promise,我有一段代码,在另一个函数中有一个异步函数。在父函数中执行return语句之前,我需要等待异步函数完成。我不熟悉promises,尽管我对stackoverflow进行了广泛的阅读,但我无法正确地获得顺序,因此在异步过程完成之前执行父函数的return语句。异步进程应该返回一个字符串,然后我想从父函数返回该字符串。我希望有人能帮我安排一下 //Call the parent function mgmtTractPopupBox.setContent(mgmtPopupContent); fun

我有一段代码,在另一个函数中有一个异步函数。在父函数中执行return语句之前,我需要等待异步函数完成。我不熟悉promises,尽管我对stackoverflow进行了广泛的阅读,但我无法正确地获得顺序,因此在异步过程完成之前执行父函数的return语句。异步进程应该返回一个字符串,然后我想从父函数返回该字符串。我希望有人能帮我安排一下

//Call the parent function
mgmtTractPopupBox.setContent(mgmtPopupContent);

function mgmtPopupContent(feature) {
    //set up query
    //Need this query to complete before executing "return content;" below
    //"relatedQuery" returns a deferred object, once that resolves, 
    //it enters the callback function.
    var content = queryableMgmtTractFL.queryRelatedFeatures(relatedQuery, relatedQueryComplete).then(function(value) {
        //This prints the result value of the relatedQuery promise (an Object), 
        //which is not what I need. I need the HTML string created in the
        //the relatedQueryComplete function.
        console.log(value);
    });
    //This executes before the asynchronous process has finished
    return content;
}

function relatedQueryComplete(relatedRecords) {
    return new Promise(function (resolve) {
        var content = '<table id="mgmtPopupTable1"><tr><th>Veg Mgmt Practice</th><th>Herbicide</th><th>Month</th><th>Year</th>\
            <th>Implemented By</th><th>Funded By</th><th>Farm Bill Code</th></tr>';
        //do stuff that adds to content variable
        content = content + '</table>';
        resolve(content);
    });
}
显然,我对承诺的理解是混乱的。有人能帮我正确地组织这个吗?另外-我确实需要向回调函数
relatedQueryComplete
传递一个额外的参数,但是
…(relatedQuery,relatedQueryComplete(extraParam))…
不起作用。我知道我需要像这样的包装

callback(function(resultFromRelatedQuery){
   relatedQueryComplete(resultFromRelatedQuery, extraParam)
});

但是,relatedQuery的
结果将最终成为
relatedQuery
延迟对象,而不是解析值。现在很困惑。

您可以使用最近添加的
async/await
功能。假设您有
firstAsyncFunction
secondAsyncFunction

async function secondAsyncFunction(param1, param2) {
    const result1 = await firstAsyncFunction();
    // do stuffs with result1, get a new result
    return resultFromSecondAsyncFunction;
}
然后您可以这样使用:

secondAsyncFunction(param1, param2).then((resultFromSecondAsyncFunction) => {
    // do stuffs with resultFromSecondAsyncFunction...
});
await
是一个关键字,它将使javascript“等待”,直到它从另一个异步函数获得结果以执行后面的操作


您可以查看有关
async/await

的更多信息,如果我能麻烦您一些,您介意给我看一下结构和我的确切函数名吗?我不明白如何以
queryablegmttractfl.queryRelatedFeatures(…)
的格式定义异步函数。鉴于我必须在平台上进行此操作,我无法更改该格式<代码>相关查询
必须在那里
queryRelatedFeatures(relatedQuery)
自动作用于
relatedQuery
,这是这个JS与之集成的程序的内置方法回调中的code>肯定会起作用。您是否向我们展示了您的实际代码?relatedQueryComplete到底在做什么?您的代码中没有异步功能。如果
mgmttractpopubox.setContent(…)
在其回调中不支持异步功能,那么您就丢失了。你对此无能为力。
secondAsyncFunction(param1, param2).then((resultFromSecondAsyncFunction) => {
    // do stuffs with resultFromSecondAsyncFunction...
});