javascript - 如何将回调函数变成 Promise
问题描述
我需要让这段代码返回一个承诺。Idk 如何做到这一点,我想学习。如果可以的话,我还需要一些解释,谢谢!
var url = "....";
function showMoviesList(callbackFunction){
fetch(url + "/movies", {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(function(response){
return response.json();
}).then(function(moviesArray){
callbackFunction(moviesArray);
});
}
解决方案
这将是
var url = "....";
function showMoviesList(callbackFunction){
return fetch(url + "/movies", {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(function(response){
return response.json();
});
}
推荐阅读
- php - 无法加载 curl.so lib(entware)
- mysql - Laravel 迁移 MySQL 外键
- django - 值错误。禁止 save() 以防止由于未保存的相关对象“fileiere”而导致数据丢失
- php - 谷歌翻译 API v2、v3 PHP
- javascript - 如何在 JS 类中有 .on('click') 事件
- python - 有没有办法在 python 中监听 firebase 数据库的变化?
- arrays - 将返回状态附加到bash中的关联数组
- scheme - 如何计算单独代码文件中 if 语句的数量
- php - 使用 php 和 mysql 从表连接中获取数据
- angular - 以反应形式注入输入并使用验证