[英]Why am I getting Promise instead of a value?
我想我不明白,為什么我會得到Promise
的回報? 我需要創建對象。 從 promise 返回值的兩個選項都不起作用。
為什么會這樣? 我錯過了什么?
解決方案:在t().then(res=>{const myVar = {...}})
創建變量
// a.js
exports.t = (key, lang, props) => {
return i18next.changeLanguage(lang).then(t => {
return t(key, props);
});
};
// b.js
import {t} from './a.js'
const myVar = {
a: "a",
b: "b",
c: (()=>{
switch (template) {
case 'a':
// Promise should return value here from t();
default:
break;
}
})(),
d: (async () => {
switch (template) {
case 'a':
// Not working, returns Promise... Why?
return await t('email.Registration Confirmation', lng);
default:
break;
}
})(),
e: (()=>{
switch (template) {
case 'a':
// Not working, returns Promise... Why?
return t('email.Registration Confirmation', lng).then(res => {
return res;
});
default:
break;
}
})()
}
有沒有可能,JavaScript 等待Promise
被解析,然后完成創建一個對象?
async
和await
是管理promise 的工具,讓您可以在async
函數中使用看似非異步的語法。
因此, async
函數將始終返回一個承諾(當它內部的所有承諾都已解析時,它將解析為任何返回值)。
您對case 'Register':
之后的功能有誤。 它返回一個承諾。 您沒有代碼來檢查它返回的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.