是否有 promise resolve return 多个值
Is there promise resolve return more than one value
function get(url) {
console.log('Making fetch() request to: ' + url);
let promise = new Promise((resolve, reject) => {
fetch(url).then(response => {`enter code here`
if (response.ok) {
const contentType = response.headers.get('Content-Type') || '';
if (contentType.includes('application/json')) {
response.json().then(obj => {
resolve(obj);
}, error => {
reject(new ResponseError('Invalid JSON: ' + error.message));
});
} else if (contentType.includes('text/html')) {
response.text().then(html => {
resolve({
page_type: 'generic',
html: html
});
}, error => {
reject(new ResponseError('HTML error: ' + error.message));
});
} else {
reject(new ResponseError('Invalid content type: ' + contentType));
}
} else {
if (response.status == 404) {
reject(new NotFoundError('Page not found: ' + url));
} else {
reject(new HttpError('HTTP error: ' + response.status));
}
}
}, error => {
reject(new NetworkError(error.message));
});
});
return promise;
}
const pobj = new Promise((resolve,reject)=> {
设置超时(()=>{
$.get("https://jsonplaceholder.typicode.com/posts1",函数(数据){
解决(数据)
}).fail((err)=>reject("Fail to load"))
},1000)
})
异步函数 abc(){
const ob1=等待 pobj;
ob1.forEach(元素 => {
document.write(element.userId +"
")
});
}
abc().catch((ERR)=>{document.write(ERR)});
// pobj.then((res)=>{
// res.forEach(元素 => {
// document.write(element.title)
// });
// }).catch((错误)=> {
// document.write(错误)
// })
function get(url) {
console.log('Making fetch() request to: ' + url);
let promise = new Promise((resolve, reject) => {
fetch(url).then(response => {`enter code here`
if (response.ok) {
const contentType = response.headers.get('Content-Type') || '';
if (contentType.includes('application/json')) {
response.json().then(obj => {
resolve(obj);
}, error => {
reject(new ResponseError('Invalid JSON: ' + error.message));
});
} else if (contentType.includes('text/html')) {
response.text().then(html => {
resolve({
page_type: 'generic',
html: html
});
}, error => {
reject(new ResponseError('HTML error: ' + error.message));
});
} else {
reject(new ResponseError('Invalid content type: ' + contentType));
}
} else {
if (response.status == 404) {
reject(new NotFoundError('Page not found: ' + url));
} else {
reject(new HttpError('HTTP error: ' + response.status));
}
}
}, error => {
reject(new NetworkError(error.message));
});
});
return promise;
}
") }); } abc().catch((ERR)=>{document.write(ERR)}); // pobj.then((res)=>{ // res.forEach(元素 => { // document.write(element.title) // }); // }).catch((错误)=> { // document.write(错误) // })