仅在promise解决后才使用值[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅在promise解决后才使用值[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我有一个app
,其中包括注册services
的async
。
我第一次使用app
之前我希望app注册所有services
。我想以某种方式返回Promise
,只有then
返回我需要的特定服务。
export function app() {
if (instance) return instance;
instance = express(feathers());
ORM.default.then((data) => {
setupServices();
instance.configure(initServices);
return instance; //this is needed before service usage
});
return instance;
}
export function service(name) {
return app().service(name);
}
其他档案:
import { service } from '../app';
const teamService = service('team');
目前teamService是null
,作为服务注册异步,并且在我第一次导入它时没有注册。
答案
您可能希望直接返回承诺
return ORM.default.then((data) => {
setupServices();
instance.configure(initServices);
return instance; //this is needed before service usage
});
这样你就可以使用app.then( ... )
了
以上是关于仅在promise解决后才使用值[重复]的主要内容,如果未能解决你的问题,请参考以下文章