MCPcopy
hub / github.com/yortus/asyncawait / next

Method next

src/async/asyncIterator.ts:30–63  ·  view source on GitHub ↗

Fetch the next result from the iterator.

(callback?: (err, result) => void)

Source from the content-addressed store, hash-verified

28
29 /** Fetch the next result from the iterator. */
30 next(callback?: (err, result) => void) {
31
32 // Configure the run context.
33 if (this._acceptsCallback) {
34 this._runContext.callback = callback; // May be null, in which case it won't be used.
35 }
36 if (this._returnValue !== Config.NONE) {
37 var resolver = defer();
38 this._runContext.resolver = resolver;
39 }
40
41 // Remove concurrency restrictions for nested calls, to avoid race conditions.
42 if (FiberMgr.isExecutingInFiber()) this._semaphore = Semaphore.unlimited;
43
44 // Run the fiber until it either yields a value or completes. For thunks, this is a lazy operation.
45 if (this._returnValue === Config.THUNK) {
46 var thunk: types.Thunk<any> = (done?) => {
47 if (done) resolver.promise.then(val => done(null, val), err => done(err));
48 this._semaphore.enter(() => this._fiber.run(this._runContext));
49 this._runContext.done = () => this._semaphore.leave();
50 };
51 } else {
52 this._semaphore.enter(() => this._fiber.run(this._runContext));
53 this._runContext.done = () => this._semaphore.leave();
54 }
55
56 // Return the appropriate value.
57 switch (this._returnValue) {
58 case Config.PROMISE: return resolver.promise;
59 case Config.THUNK: return thunk;
60 case Config.RESULT: return await (resolver.promise);
61 case Config.NONE: return;
62 }
63 }
64
65 /** Enumerate the entire iterator, calling callback with each result. */
66 forEach(callback: (value) => void, doneCallback?: (err?) => void): any {

Callers 4

forEachMethod · 0.95
async.iterable.jsFile · 0.80
async.iterable.tsFile · 0.80
asyncIterator.jsFile · 0.80

Calls 4

enterMethod · 0.80
leaveMethod · 0.80
deferFunction · 0.70
runMethod · 0.65

Tested by

no test coverage detected