1
약속에 jsonp 요청을 래핑했습니다. 일련의 API 호출을 작성했습니다. 일단 모든 API 호출이 해결되면 처리를 계속 진행하고 싶습니다.Promise.all returns undefined
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "habathcx", "RobotCaleb", "noobs2ninjas", "twitchpresents"]
Promise.all(users.map(function(user){
var url = "https://wind-bow.gomix.me/twitch-api/users/" + user;
return jsonp(url);
})).then(function(arr){
console.log(arr);
})
PromiseValue가 정의되지 않은 약속 배열을 반환하므로 더 이상 값을 처리 할 수 없습니다.
var count = 0;
var jsonp = function(url, options) {
options = options || {};
var prefix = options.prefix || '__jp';
var param = options.param || 'callback';
var timeout = options.timeout ? options.timeout : 15000;
var target = document.getElementsByTagName('script')[0] || document.head;
var script;
var timer;
var cleanup;
var cancel;
var promise;
var noop = function() {};
// Generate a unique id for the request.
var id = prefix + (count++);
cleanup = function() {
// Remove the script tag.
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
window[id] = noop;
if (timer) {
clearTimeout(timer);
}
};
promise = new Promise(function(resolve, reject) {
if (timeout) {
timer = setTimeout(function() {
cleanup();
reject(new Error('Timeout'));
}, timeout);
}
window[id] = function(data) {
cleanup();
resolve(data);
};
// Add querystring component
url += (~url.indexOf('?') ? '&' : '?') + param + '=' + encodeURIComponent(id);
url = url.replace('?&', '?');
// Create script.
script = document.createElement('script');
script.src = url;
target.parentNode.insertBefore(script, target);
cancel = function() {
if (window[id]) {
cleanup();
reject(new Error('Canceled'));
}
};
});
return {
promise: promise.then(function(data){
var user = {
name: data.name,
logo: data.logo,
};
userData.push(user);
}),
cancel: cancel
};
}
내가 각각의 API 호출 내에서 결과를 밀어 수 있지만 Promise.All에서 값을 액세스하는 방법이 있습니다 :
내 JSONP 약속 래퍼가 여기에있다?