-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer importing ky-universal until actually needed
This accomplishes two things: * It prevents a dangling promise with an import in it. If the module is loaded (queuing up the import promise) and then no clients are ever used, and thus never awaited, the import can happen too late. In particular, Jest will break if the import happens after the test is complete. * It avoids a dynamic import altogether when the client isn't needed. In particular, Jest uses Node's VM API which only experimentally supports dynamic imports (and ESM modules altogether). This change means that merely loading the http-client module doesn't require enabling that experimental support.
- Loading branch information
Showing
3 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export function deferred(f) { | ||
let promise; | ||
|
||
return { | ||
then( | ||
onfulfilled, | ||
onrejected | ||
) { | ||
promise ||= new Promise(resolve => resolve(f())); | ||
return promise.then( | ||
onfulfilled, | ||
onrejected | ||
); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {deferred} from '../lib/deferred.js'; | ||
|
||
describe('deferred()', () => { | ||
it('resolves to the return value of its function', async () => { | ||
const d = deferred(() => { | ||
return 'return value'; | ||
}); | ||
|
||
const ret = await d; | ||
ret.should.equal('return value'); | ||
}); | ||
|
||
it('defers execution until awaited', async () => { | ||
let executionCount = 0; | ||
executionCount.should.equal(0); | ||
|
||
const d = deferred(() => { | ||
executionCount++; | ||
return 'return value'; | ||
}); | ||
|
||
executionCount.should.equal(0); | ||
await d; | ||
executionCount.should.equal(1); | ||
}); | ||
|
||
it('only executes once', async () => { | ||
let executionCount = 0; | ||
executionCount.should.equal(0); | ||
|
||
const d = deferred(() => { | ||
executionCount++; | ||
return 'return value'; | ||
}); | ||
|
||
await d; | ||
await d; | ||
executionCount.should.equal(1); | ||
}); | ||
|
||
it('unwraps returned promises', async () => { | ||
const d = deferred(() => { | ||
return Promise.resolve('return value'); | ||
}); | ||
|
||
const ret = await d; | ||
ret.should.equal('return value'); | ||
}); | ||
}); | ||
|