You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+88-1Lines changed: 88 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -113,6 +113,93 @@ increment();
113
113
// Prints `counter value is 3` since it waits for the 3rd call before resolving the Promise.
114
114
```
115
115
116
+
### nextCallDuring(fn)
117
+
118
+
Wait for the function to be called from a callback.
119
+
120
+
```js
121
+
let counter =0;
122
+
123
+
constincrement=anticipatedCall(() => {
124
+
counter = counter +1;
125
+
});
126
+
127
+
increment.nextCallDuring(() => {
128
+
counter =5;
129
+
increment();
130
+
}).then(() =>console.log(`counter value is ${counter}`));
131
+
// Prints `counter value is 6`
132
+
```
133
+
134
+
### nthCallDuring(n, fn)
135
+
136
+
Like `nextCallDuring()`, but wait for the function to be called `n` times.
137
+
138
+
139
+
```js
140
+
let counter =0;
141
+
142
+
constincrement=anticipatedCall(() => {
143
+
counter = counter +1;
144
+
});
145
+
146
+
increment.nthCallDuring(3, () => {
147
+
counter =5;
148
+
increment();
149
+
increment();
150
+
increment();
151
+
}).then(() =>console.log(`counter value is ${counter}`));
152
+
// Prints `counter value is 8`
153
+
```
154
+
155
+
## Usage note
156
+
157
+
In some cases, it's important to keep in mind that the returned Promise will resolve _at the end of the call frame_. Resolving a Promise is an asynchronous operation. Since JavaScript does one thing at a time (for the most part), when the Promise is resolved, it waits for the currently running function to stop executing (for a regular function, this happens on return; for an `async` function, this happens on `await`). If you're tracking state -- for instance, using a variable in a closure -- you might get unexpected results.
158
+
159
+
Here's an example:
160
+
161
+
```js
162
+
let counter =0;
163
+
164
+
constincrement=anticipatedCall(() => {
165
+
counter = counter +1;
166
+
});
167
+
168
+
increment.nextCallDuring(() => {
169
+
increment();
170
+
increment();
171
+
increment();
172
+
}).then(() =>console.log(`counter value is ${counter}`));
173
+
// Prints `counter value is 3`... but why?
174
+
```
175
+
176
+
`anticipated-call` was told to wait for the next invocation, but it didn't return until `increment()` had been called _three_ times! This is because the callback inside `nextCallDuring` needed to complete execution before the Promise could resolve.
177
+
178
+
If the callback were an asynchronous function that yielded execution after the call, it would behave as might be expected:
}).then(() =>console.log(`counter value is ${counter}`));
196
+
// Prints `counter value is 1`
197
+
```
198
+
199
+
The purpose of introducing `delay(0)` is to interrupt the call frame to allow the `anticipated-call` to have a chance to respond.
200
+
201
+
If you're interested in learning more, I suggest reading about the JavaScript event loop ([this article](https://hackernoon.com/understanding-js-the-event-loop-959beae3ac40) is a great start).
202
+
116
203
## Contributing
117
204
118
-
This project uses [ESLint-style commit messages](https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-eslint/convention.md).
205
+
This project uses [ESLint-style commit messages](https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-eslint/readme.md).
0 commit comments