最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: resolve is not a function - Stack Overflow

programmeradmin3浏览0评论

I am trying to write a function that will read a file and depending upon success or failure in reading it will call the resolve or reject function. Below is the code that I have written.

let fs = require('fs');

const FILE_NAME = './assets/pies.json';

let pieRepo = {
    get: function(resolve, reject) {
        fs.readFile(FILE_NAME, function(err, data) {
            if(err) {
                reject(err);
            }
            else {
                resolve(JSON.parse(data));
            }
        });
    }
};

module.exports = pieRepo;

However when I run "npm start" it throws the below error

/workingdirectory/repos/pieRepo.js:12
                resolve(JSON.parse(data));
                ^

TypeError: resolve is not a function
    at /workingdirectory/repos/pieRepo.js:12:17
    at FSReqWrap.readFileAfterClose [as onplete] (internal/fs/read_file_context.js:53:3)

I am using version 16.11.0 of node.

I am trying to write a function that will read a file and depending upon success or failure in reading it will call the resolve or reject function. Below is the code that I have written.

let fs = require('fs');

const FILE_NAME = './assets/pies.json';

let pieRepo = {
    get: function(resolve, reject) {
        fs.readFile(FILE_NAME, function(err, data) {
            if(err) {
                reject(err);
            }
            else {
                resolve(JSON.parse(data));
            }
        });
    }
};

module.exports = pieRepo;

However when I run "npm start" it throws the below error

/workingdirectory/repos/pieRepo.js:12
                resolve(JSON.parse(data));
                ^

TypeError: resolve is not a function
    at /workingdirectory/repos/pieRepo.js:12:17
    at FSReqWrap.readFileAfterClose [as onplete] (internal/fs/read_file_context.js:53:3)

I am using version 16.11.0 of node.

Share Improve this question asked Apr 21, 2022 at 8:42 sumankharasumankhara 311 gold badge1 silver badge3 bronze badges 1
  • 3 resolve() and reject() are parameters of a Promise, that you're not using on your code sample – Rafael Francisco Commented Apr 21, 2022 at 8:44
Add a ment  | 

4 Answers 4

Reset to default 2

The issue you are facing is with the way you are trying to resolve promise. The Resolve & Reject Functions are available within Promise

let fs = require("fs");

const FILE_NAME = "YOUR PATH TO FILE";

let pieRepo = {
  get: () => {
    return new Promise((resolve, reject) => {
      fs.readFile(FILE_NAME, (err, data) => {
        if (err) {
          return reject(err);
        }
        resolve(JSON.parse(data));
      });
    });
  },
};

module.exports = pieRepo;

You can further read about Promise

Resolve and reject are functions available in promises.

Replace your code with this:

let fs = require('fs');

const FILE_NAME = './assets/pies.json';

let pieRepo = {
    get: () => {
        return new Promise((resolve, reject) => {
            fs.readFile(FILE_NAME, (err, data) => {
                if (err) {
                    return reject(err);
                }
                resolve(JSON.parse(data));
            });
        });
    },
};

module.exports = pieRepo;

Now, you can await the get function.

If this were my project though, the code would look like this:

let fs = require('fs/promises');

const FILE_NAME = './assets/pies.json';

let pieRepo = {
    get: async () => {
        try {
            const data = await fs.readFile(FILE_NAME);
            return data;
        } catch (err) {
            throw new Error(err.message);
        }
    },
};

module.exports = pieRepo;

If you want to promisify fs.readFile, you can, but promisified versions of fs functions are available through fs/promises.

Okay, so I know precisely where you're getting this code from on pluralsight, I hit the same thing. Your original code is fine...IF and ONLY IFF you have removed the //let pies = pieRepo.get();

In your index.js. Otherwise it's making an invalid call and is going to barf.

You have to use Promise, here is a very simple example:

test() {
    return new Promise((resolve, reject) => {
      if (1 === 1) {
        resolve(true)
      } else {
        reject(false)
      }
    })
  }
发布评论

评论列表(0)

  1. 暂无评论