我在使用 promise 等待函数结束后再返回值时遇到问题。我已经多次查看这个论坛的回复,但无法弄清楚如何让它发挥作用。
帖子> How do I return the response from an asynchronous call?
我得到的最接近的是这个,但它仍然不起作用。如果有人能纠正我的错误,我将不胜感激。
来自 Blazor:
string thing = await js.InvokeAsync<string>("GetThing");
至 JavaScript:
window.CreateFontThumnailArray = () =>
{
var thing = "";
let start = new Promise(function (resolve, reject) {
someObject.GetThingWithCallback(function (blob) {
thing = "some text";
//I want this to finish before parent function completes,
// and "return thing;" is called.
resolve();
});
});
Promise.all([start]);
return thing;
}
我理解了上述帖子中的一些内容,并设法使一些测试函数起作用,但在函数的回调中却不起作用。喜欢上面的示例“GetThingWithCallback(function(){"HERE"})”
我唯一的其他选择是将其设为无效调用。然后处理 Blazor 端的回调。 但如果我这样做,我将无法处理调用 JavaScript 函数后立即执行的操作。这将是最理想的。
非常感谢任何帮助。
最佳答案
我设法解决了这个问题,以防万一有人也需要这样做..
我已经很接近了,我只需更改为异步函数即可。 并更改 Promise.all([start]);等待开始;
window.CreateFontThumnailArray = async () =>
{
var thing = "";
let start = new Promise(function (resolve, reject) {
someObject.GetThingWithCallback(function (blob) {
thing = "some text";
//I want this to finish before parent function completes,
// and "return thing;" is called.
resolve();
});
});
await start;
return thing;
}
关于javascript - Blazor JS 返回不适用于嵌入功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60593689/