相关疑难解决方法(0)

如何从异步调用返回响应?

我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo

我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });

    return result;
}

var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery asynchronous xmlhttprequest

5208
推荐指数
38
解决办法
134万
查看次数

Firebase Firestore get() 异步/等待

谁能帮我用 async/await 在 Typescript 中“翻译”这个例子

console.log("start") 
var citiesRef = db.collection('cities');
var allCities = citiesRef.get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data().name);
        });
        console.log("end")
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });
Run Code Online (Sandbox Code Playgroud)

我测试了一些代码,但我认为我在“forEach”循环上做错了。

我想要在控制台中的结果:

start
Key1 => city1
Key2 => city2
end
Run Code Online (Sandbox Code Playgroud)

结果我参加了一些测试:

start
end
Key1 => city1
Key2 => city2
Run Code Online (Sandbox Code Playgroud)

提前谢谢

typescript google-cloud-firestore

9
推荐指数
1
解决办法
2万
查看次数