Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Last active September 21, 2018 22:23
Show Gist options
  • Save vinicius73/36bd0e66b48d948d7168a193ea4967be to your computer and use it in GitHub Desktop.
Save vinicius73/36bd0e66b48d948d7168a193ea4967be to your computer and use it in GitHub Desktop.
$nextTickInSequence
import $nextTickInSequence from './next-tick-in-sequence'
export const install = Vue => {
Object.defineProperty(Vue.prototype, `$nextTickInSequence`, {
get: () => $nextTickInSequence
})
}
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