promise and priority
ใชใใ้
ๅปถใใพใกใใฃใๅชๅ
ๅบฆใใใใจใerror ใ็บ็ใใใใจใๅบฆใ
ใใใพใใ
ไปๅใฏ promise
ใจ priority
ใงใๅๆ็ใใค้ ็ชใซๅฎ่กใงใใใใใซใใพใใ
codesandbox ใฎ demo ใใใใฎใงใใใใใใใฐใใใใงใฟใฆใใ ใใใ
promiseAtomโ
promise.then
ใใ function ใ่ฟฝๅ ใใใใจใงใๅๆๅฎ่กใงใใใใใซใชใใพใใ
demoใงใฏใblock ใ interact ใใ้ ใซ fade out ใใฆใใใพใใ
promiseAtom test - CodeSandbox

then
ใใ function ใ่ฟฝๅ ใใๆๆฐใฎ Promise
ใcurrent ใซไฟๅญใใพใใ
function makePromise(executor = (resolve) => resolve()) {
let current = new Promise(executor);
return {
finally: (fun) => (current = current.finally(fun)),
catch: (fun) => (current = current.catch(fun)),
then: (fun) => (current = current.then(fun))
};
}
priorityAtomโ
priority number ใซใใใๅๆ็ใใค้ ๆฌกๅฎ่กใใงใใใใใซใชใใพใใ
demo ใงใฏใ้ซใ y
ใฏ priority number ใชใฎใง้ซใใใญใใฏใใๆถใใพใใ
priorityAtom test - CodeSandbox

function ใฎ่ฟฝๅ ใใจใซ sort ใใๅฎ่กๆใฏๅ
้ ญใฎ function ใๅฎ่กใใพใใ
binary heep
ใง function ใ sort ใใใจใใใ performance tuning ใงใใใใงใใ
function makePriority(executor = (resolve) => resolve()) {
const promise = makePromise(executor);
const current = [];
return {
finally: (fun) => promise.finally(fun),
catch: (fun) => promise.catch(fun),
then: (fun, priority = -1) => {
current.push([priority, fun]);
current.sort(([i], [j]) => i - j);
promise.then(async () => await current.shift()[1]());
return promise;
}
};
}