stub.callsArgOnAsync(index, object)
Causes the stub to call the argument at the provided index as a callback function, with an additional object parameter to pass the this context, asynchronously.
js
import * as sinon from "sinon";
const car = {
color: "red"
};
const stub = sinon.stub().callsArgOnAsync(0, car);
function updateColor() {
this.color = "blue";
}
console.log(car.color);
// => red
stub(updateColor);
console.log(car.color);
// => red
setTimeout(function () {
console.log(car.color);
// => blue
}, 1);Errors
When the argument at the provided index is undefined, or not a function, an Error will be thrown.
js
import * as sinon from "sinon";
const car = {
color: "red"
};
const stub = sinon.stub().callsArgOnAsync(0, car);
const pie = "apple pie";
stub(pie);
// => Uncaught TypeError: argument at index 0 is not a function: apple pie
stub(nonExistingCallback);
// => Uncaught ReferenceError: nonExistingCallback is not definedSee also
- stub.callsArg
- stub.callsArgAsync
- stub.callsArgOn
- stub.callsArgOnWith
- stub.callsArgOnWithAsync
- stub.callsArgWith
- stub.callsArgWithAsync
More information
- https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
- https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
- YouTube: "JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue" by Lydia Hallie
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
