Skip to content

Commit 36457b9

Browse files
committed
handle nextTick
1 parent 449e510 commit 36457b9

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/async.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,22 @@ function checkNative (algo) {
3939
checks[algo] = prom
4040
return prom
4141
}
42-
42+
var nextTick
43+
function getNextTick () {
44+
if (nextTick) {
45+
return nextTick
46+
}
47+
if (global.process && global.process.nextTick) {
48+
nextTick = global.process.nextTick
49+
} else if (global.queueMicrotask) {
50+
nextTick = global.queueMicrotask
51+
} else if (global.setImmediate) {
52+
nextTick = global.setImmediate
53+
} else {
54+
nextTick = global.setTimeout
55+
}
56+
return nextTick
57+
}
4358
function browserPbkdf2 (password, salt, iterations, length, algo) {
4459
return subtle.importKey(
4560
'raw', password, { name: 'PBKDF2' }, false, ['deriveBits']
@@ -59,11 +74,11 @@ function browserPbkdf2 (password, salt, iterations, length, algo) {
5974

6075
function resolvePromise (promise, callback) {
6176
promise.then(function (out) {
62-
process.nextTick(function () {
77+
getNextTick()(function () {
6378
callback(null, out)
6479
})
6580
}, function (e) {
66-
process.nextTick(function () {
81+
getNextTick()(function () {
6782
callback(e)
6883
})
6984
})
@@ -78,7 +93,7 @@ module.exports = function (password, salt, iterations, keylen, digest, callback)
7893
var algo = toBrowser[digest.toLowerCase()]
7994

8095
if (!algo || typeof global.Promise !== 'function') {
81-
return process.nextTick(function () {
96+
return getNextTick()(function () {
8297
var out
8398
try {
8499
out = sync(password, salt, iterations, keylen, digest)

0 commit comments

Comments
 (0)