Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it may be good to fix sth. for chapter04 promisify.js #19

Closed
YMC-GitHub opened this issue Sep 21, 2018 · 0 comments
Closed

it may be good to fix sth. for chapter04 promisify.js #19

YMC-GitHub opened this issue Sep 21, 2018 · 0 comments
Assignees

Comments

@YMC-GitHub
Copy link

YMC-GitHub commented Sep 21, 2018

i run cmd with your code:

$ node spider.js http://frps.eflora.cn/frps/Rosales

then a err comes--the file content i write to is undefined{1} .

function dowload(){
//...
 writeFile(filename, body))//{1}
//...
}

i think it may be good to change sth in :

  • add another _agruments to save the aguments for function promisified({1})
  • use the var at ({2},{3}) ,not agruments
    since here a function too, it may have its own arguments ({0})

-or change (not testing) args.push(function(err, result) { ...} to args.push((err, result)=>{...}

function promisify (callbackBasedApi) {
  return function promisified() {
    const args = [].slice.call(arguments);
    const _arguments = arguments; //{1}
    return new Promise((resolve, reject) => {  
      args.push(function(err, result) {     //{0}
        if(err) {
          return reject(err);       
        }
        if(_arguments.length <= 1) {       //{2}
          resolve(result);
        } else {
          resolve([].slice.call(_arguments, 1)); //{3}
        }
      });
      callbackBasedApi.apply(null, args);    
    });
  }
};

it may lead to the result of request operation in download function:

function dowload(){
//...
return request(url)
    .then(response => {
      body = response.body;
      //console.log(JSON.stringify(response));
     //the response is an array with length 2 . But it expect to an object since `request(url)`
    })
//...
}

thanky you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants