This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = 'init' | |
const func = () => { | |
str='changed' | |
} | |
setTimeout(func,3000) | |
console.log(str) // '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = {} | |
//使用 async 將 ajax 請求改為同步。 | |
$.ajax({ | |
url: '', | |
async: false, | |
success: (status)=>{ | |
data = status | |
} | |
)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
/**成功時回傳**/ | |
resolve(status) | |
/**失敗時回傳**/ | |
reject(status) | |
}).then((data)=>{ | |
/**以 then 接續成功時的處理**/ | |
}).catch((error)=>{ | |
/**以 catch 接續失敗時的處理**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('changed')}, 3000) | |
}).then((data)=>{ | |
console.log(data) //3 秒後執行,並印出'changed' | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{ | |
if(Math.random() > 0.5){ | |
resolve('changed') | |
} | |
else{ | |
reject('error') | |
} | |
}, 3000) | |
}).then((data)=>{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('changed')}, 3000) | |
}).then((data)=>{ | |
console.log(data) // 'changed' | |
return 'last changed' | |
}).then((data)=>{ | |
console.log(data) // 'last changed' | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('changed')}, 3000) | |
}).then((data)=>{ | |
console.log(data) | |
setTimeout(()=>{data = 'last changed'},2000) | |
return data | |
}).then((data)=>{ | |
console.log(data) // 'changed' | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lastPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('last changed')},2000) | |
}) | |
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('changed')}, 3000) | |
}).then((data)=>{ | |
console.log(data) | |
return lastPromise.then((lastData)=>{return lastData}) | |
}).then((data)=>{ | |
console.log(data) // 'last changed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newPromise = new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('changed')}, 3000) | |
}).then((data)=>{ | |
console.log(data) | |
//第二個Promise | |
return new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('last ' + data)},2000) | |
}).then((lastData)=>{ | |
return lastData | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initState = { | |
name: 'Jack', | |
} |
OlderNewer