我正在尝试使我的代码使用诺言。
读取和播放后,尝试在“ githubUserData”对象中设置一些属性,从而创建了随后的代码。Promise似乎正在运行,但“ githubUserData”返回{}(空)
显然,如我的控制台日志所示,我的代码未按期望的顺序执行。它在设置属性之前显示该对象。
console.clear();
console.log( '>>>>>> Starting promise..' );
// Create object to keep requested data
let githubUserData = {};
function getGithubUserData() {
// Promise
return new Promise( ( resolve, reject ) => {
$.ajax({
url: 'https://api.github.com/users/codepen',
data: {},
success: function( data ) {
resolve( data ); // Resolve promise and go to then()
},
error: function( err ) {
reject( err ); // Reject the promise and go to catch()
}
});
});
};
getGithubUserData().then( data => …Run Code Online (Sandbox Code Playgroud)