【发布时间】:2019-04-29 02:57:24
【问题描述】:
我想在解决承诺后返回要在父级中呈现的模板。我知道不能从承诺中返回价值。收到模板数据后如何渲染模板?
在以下示例中,ContentPane 是列出所有要呈现的模板的父级。在电影中,会进行网络调用,因此需要渲染模板。
ContentPane.prototype.getTemplate = function(){
let template = `
<div class="contentPane" id="contentPane">
${new Films().render()}
</div>
`;
return template;
}
Films.prototype.render = function(){
var template = this.getTemplate();
template.then(function(val){
return val;
})
//based on the value resolved by promise,
//appropriate template should be returned to parent
}
Films.prototype.getTemplate = async function(){
//make network call
//create template based on server response
}
【问题讨论】:
-
请注意,函数
getTemplate不返回承诺。它只是以同步方式返回模板字符串。因此你不能在上面使用.than() -
@NirWeber 我已经用 getTemplate 函数更新了问题
标签: javascript ecmascript-6 es6-promise