0
我從ajax返回一個Promise API。我想從然後返回()這樣的函數.. 我阿賈克斯funcation:如何將Promise稱爲值?
function get_data(url, id) {
return new Promise(function (resolve, reject) {
xmlHttp.open("GET", url + "/" + id, true);
xmlHttp.onerror = reject;
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
resolve(JSON.parse(this.responseText));
}
};
xmlHttp.send(null);
});
}
調用Ajax的功能:
var class_name = '';
var url = site_url + "/student/get_classes";
var id = 2;
class_name = get_data(url, id).then(function (result) {
console.log(result);//first console
return result.name;
});
console.log(class_name);//second console
我的控制檯:
第一個控制檯:Object {id: "1", name: "Class One", shift_id: "1", session_id: "1"}
第二個控制檯:Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
如何顯示[[PromiseValue]]窗體Promise Object。
它是否可行?提前致謝。