Skip to content

Commit

Permalink
closed #5251
Browse files Browse the repository at this point in the history
  • Loading branch information
wnghdcjfe committed Dec 9, 2024
1 parent f729cd0 commit 5c4da0d
Showing 1 changed file with 62 additions and 34 deletions.
96 changes: 62 additions & 34 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,46 +347,74 @@ Runnable.prototype.run = function (fn) {
return;
}

// sync or promise-returning
try {
callFn(this.fn);
} catch (err) {
errorWasHandled = true;
if (err instanceof Pending) {
return done();
} else if (this.allowUncaught) {
throw err;
}
done(Runnable.toValueOrError(err));
}
Promise.resolve()
.then(() => callFn(fn))
.catch((err) => handleError(err));

function callFn(fn) {
var result = fn.call(ctx);
if (result && typeof result.then === 'function') {
self.resetTimeout();
result.then(
function () {
done();
// Return null so libraries like bluebird do not warn about
// subsequently constructed Promises.
return null;
},
function (reason) {
done(reason || new Error('Promise rejected with no or falsy reason'));
function callFn(fn) {
try {
const result = fn.call(ctx, function (err) {
if (err instanceof Error || toString.call(err) === '[object Error]') {
return done(err); // Error 객체 처리
}
);
} else {
if (self.asyncOnly) {
return done(
new Error(
if (err) {
if (Object.prototype.toString.call(err) === '[object Object]') {
return done(
new Error('done() invoked with non-Error: ' + JSON.stringify(err))
);
}
return done(new Error('done() invoked with non-Error: ' + err));
}
if (result && utils.isPromise(result)) {
return done(
new Error(
'Resolution method is overspecified. Specify a callback *or* return a Promise; not both.'
)
);
}
done();
});

if (result && typeof result.then === 'function') {
self.resetTimeout();

return result
.then(() => {
done();
// escape Bluebird
return null;
})
.catch((reason) => {
throw reason || new Error('Promise rejected with no or falsy reason');
});
} else {
if (self.asyncOnly) {
throw new Error(
'--async-only option in use without declaring `done()` or returning a promise'
)
);
);
}
done();
}

done();
} catch (err) {
handleError(err);
}
}


function handleError(err) {
errorWasHandled = true;

if (err instanceof Pending) {
return done(); // Pending 예외 처리
}

if (self.allowUncaught) {
throw err; // uncaught 에러 허용 시 throw
}

done(Runnable.toValueOrError(err)); // 나머지 에러 처리
}


function callFnAsync(fn) {
var result = fn.call(ctx, function (err) {
Expand Down

0 comments on commit 5c4da0d

Please sign in to comment.