Last active
September 21, 2018 22:23
-
-
Save vinicius73/36bd0e66b48d948d7168a193ea4967be to your computer and use it in GitHub Desktop.
$nextTickInSequence
This file contains hidden or 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
import $nextTickInSequence from './next-tick-in-sequence' | |
export const install = Vue => { | |
Object.defineProperty(Vue.prototype, `$nextTickInSequence`, { | |
get: () => $nextTickInSequence | |
}) | |
} |
This file contains hidden or 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
import { isFunction, castArray, isArray } from 'lodash' | |
import { requestAnimFrame } from './utils/fn' | |
function nextTick (callback) { | |
this.$nextTick(function onNextTick () { | |
requestAnimFrame(callback) | |
}) | |
} | |
function $nextTickInSequence (...fns) { | |
const fnList = [...castArray(fns)].reverse() | |
// call next function | |
const callNext = () => { | |
const next = fnList.pop() | |
// if next is a array of functions | |
if (isArray(next)) { | |
return $nextTickInSequence.apply(this, next) | |
} | |
// call function and callNext | |
if (isFunction(next)) { | |
return new Promise((resolve, reject) => { | |
nextTick.call(this, () => { | |
try { | |
Promise | |
.resolve(next()) | |
.then(() => { | |
resolve(callNext()) | |
}) | |
} catch (e) { | |
console.warn('nextTickInSequence', e) | |
reject(e) | |
} | |
}) | |
}) | |
} | |
// finish | |
return Promise.resolve() | |
} | |
return callNext() | |
} | |
export default $nextTickInSequence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment