Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c1613c0
internal: add {callback,promis}ify.js
chrisdickinson Feb 1, 2016
a8efd4f
zlib: promisify
chrisdickinson Feb 1, 2016
4af7ec2
repl: promisify
chrisdickinson Feb 1, 2016
0e0a2d9
readline: promisify .question + callbackify completer
chrisdickinson Feb 1, 2016
8798a26
build: add callbackify and promisify to node.gyp
chrisdickinson Feb 1, 2016
355205b
net: promisify socket.setTimeout; add connectAsync
chrisdickinson Feb 1, 2016
7b8e57d
internal: fixup promisify
chrisdickinson Feb 1, 2016
3f2b0d8
http,https: add {request,get}Async
chrisdickinson Feb 1, 2016
23ba073
fs: no callback -> return promise
chrisdickinson Feb 1, 2016
68837b8
dns: promisify
chrisdickinson Feb 1, 2016
3317652
dgram: promisify .{bind,close,send}
chrisdickinson Feb 1, 2016
846bd49
doc,dgram: add promise notes to dgram docs
chrisdickinson Feb 1, 2016
d90b42c
internal: add hooks for specifying different promise resolver
chrisdickinson Feb 1, 2016
adbe352
crypto: promisify pbkdf2, add randomBytesAsync
chrisdickinson Feb 1, 2016
697ce01
child_process,cluster: promisify .send(), add exec{File,}Async
chrisdickinson Feb 1, 2016
f36b62d
add process.setPromiseImplementation
chrisdickinson Feb 1, 2016
082fa08
process: add setPromiseImplementation API
chrisdickinson Feb 1, 2016
54e3001
tls,net: connectAsync should resolve to Socket
chrisdickinson Feb 1, 2016
014fb3e
lib: repromisify
chrisdickinson Feb 2, 2016
15a42c1
repromisify
chrisdickinson Feb 2, 2016
8586b10
src,lib: move back to native promises
chrisdickinson Feb 2, 2016
40ca55e
domain,promise,src: add shim to make promises work with async_wrap + …
chrisdickinson Feb 2, 2016
3928beb
domain: promises capture process.domain at .then time
chrisdickinson Feb 2, 2016
fa725b3
src: back asyncwrap integration out of this pr
chrisdickinson Feb 2, 2016
0528d74
internal: lint
chrisdickinson Feb 3, 2016
010224a
http{,s}: getAsync only listens for error once
chrisdickinson Feb 5, 2016
6ae09c5
http{s,}: nix {request,get}Async
chrisdickinson Feb 10, 2016
670a9c2
rework promisifier
chrisdickinson Feb 10, 2016
240c72d
src: add flag
chrisdickinson Feb 10, 2016
565dfe2
fix handler
chrisdickinson Feb 11, 2016
3384ab0
introduce .promised
chrisdickinson Feb 11, 2016
4e38057
bugfix: use outer arguments
chrisdickinson Feb 11, 2016
8c97549
wip: add recovery object
chrisdickinson Feb 11, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
process: add setPromiseImplementation API
  • Loading branch information
chrisdickinson committed Feb 1, 2016
commit 082fa0801032e98650f8c67e113b701f8195508d
23 changes: 23 additions & 0 deletions doc/api/process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,29 @@ need to be root or have the `CAP_SETGID` capability.

The list can contain group IDs, group names or both.

## process.setPromiseImplementation(impl)

Sets the [`Promise`][def-promises] implementation for the process. All promises
returned by core APIs will be resolved by this implementation, giving third
party promise implementations an opportunity to cast native promises into a
different type.

**Only top-level applications should use this API.** If this API is called more
than once, subsequent calls will issue deprecation warnings so authors can
track down offending modules.

```javascript
process.setPromiseImplementation(require('bluebird'));

const fs = require('fs');

fs.readFile('/usr/share/dict/words', 'utf8').then((words) => {
console.log(`a picture is worth ${words.split('\n').length} words.`);
}, (err) => {
console.error('there are no words.');
});
```

## process.setuid(id)

Note: this function is only available on POSIX platforms (i.e. not Windows,
Expand Down
20 changes: 10 additions & 10 deletions lib/internal/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,10 @@ function setResolver(wrapper) {

function setup(process) {
var implSet = false;
var where = null
var where = null;
process.setPromiseImplementation = function setImpl(impl) {
const assert = require('assert');
const util = require('internal/util');
if (!implSet) {
implSet = true;
where = new Error('setPromiseImplementation');
process.setPromiseImplementation = util.deprecate(
setImpl,
'setPromiseImplementation has already been called for this process:\n' +
where.stack + '\n'
);
}
assert(
(typeof impl === 'object' || typeof impl === 'function') && impl,
'implementation should be a truthy object'
Expand All @@ -50,6 +41,15 @@ function setup(process) {
test && typeof test.then === 'function',
'.resolve should return a thenable'
);
if (!implSet) {
implSet = true;
where = new Error('setPromiseImplementation');
process.setPromiseImplementation = util.deprecate(
setImpl,
'setPromiseImplementation has already been called for this process:\n' +
where.stack + '\n'
);
}
setResolver(impl);
};
}
58 changes: 58 additions & 0 deletions test/parallel/test-process-set-promises-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

require('../common');

const fs = require('fs');
var assert = require('assert');

assert.throws(() => {
process.setPromiseImplementation(null);
});

assert.throws(() => {
process.setPromiseImplementation('ok');
});

assert.throws(() => {
process.setPromiseImplementation({});
});

assert.throws(() => {
process.setPromiseImplementation({
resolve() {
}
});
});

assert.throws(() => {
process.setPromiseImplementation({
resolve() {
return {};
}
});
});

process.setPromiseImplementation({
resolve(promise) {
return {
hello: 'world',
then(fn0, fn1) {
return promise.then(fn0, fn1);
}
};
}
});

// affects old requires:
const p = fs.readFile(__filename, 'utf8');
p.then((lines) => {
assert.ok(lines.indexOf("'use strict'") !== -1);
});
assert.equal(p.hello, 'world');

// affects new requires:
const r = require('crypto').randomBytesAsync(16);
r.then((bytes) => {
assert.ok(Buffer.isBuffer(bytes));
});
assert.equal(r.hello, 'world');