Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix(node): node patched method should copy original delegate's symbol properties #1095

Merged
merged 1 commit into from
Jun 18, 2018

Conversation

JiaLiPassion
Copy link
Collaborator

fix #1094

the error can be described as this,

    import * as util from 'util';
    const promisifyExists = util.promisify(exists);
    promisifyExists(__filename).then(r => {
      expect(r).toBe(true);
      done();
    }, err => {
      fail(`should not be here with error: ${err}`);
    });

Before this PR, after promisify, the promise will be rejected as error.
The reason is in nodejs promisify implementation, by default, it will handle the nodejs style callback (err, ...values) to promise, but some of the nodejs API is not in this style, such as fs.exists, the callback is (exists) => {}, without the 1st err parameter.

So nodejs provide a mechanism to let those special API provide their own promisify implementation by add a symbol property Symbol(custom) in the method, for example, fs.exists provide a custom implementation here, https://github.com/nodejs/node/blob/master/lib/fs.js#L213

Object.defineProperty(exists, internalUtil.promisify.custom, {
  value: (path) => {
    const { createPromise, promiseResolve } = process.binding('util');
    const promise = createPromise();
    fs.exists(path, (exists) => promiseResolve(promise, exists));
    return promise;
  }
});

And this property is defined on the function, so when zone.js patched this function, the symbol property got lost, so we need to copy this symbol property.

@mhevery mhevery merged commit 0a2f6ff into angular:master Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

zone-node breaks util.promisify
3 participants