Converting Promises Code to async await and mocking test cases?

橙三吉。 提交于 2020-02-24 20:23:49

问题


How to convert the below function which has nested promise and await to just using await or only using promises ?

const test = (url, id) => {
  return new Promise((_resolve, _reject) => {
    if (isValidUrl(url)) {
      let storage = new Storage(Indexdb, id);
      const cae = new valueExtract(url);
      cae.fetch()
        .then(data => {
          new zip(data)
            .then(obj => obj.getZip())
            .then(obj => obj.getList())
            .then(list => {
              return new Promise(async (resolve, reject) => {
                try {
                  let sI = storage.connection;
                  await Promise.all(Object.keys(list).map(async (fileName, index) => {
                    let blob = await new FileExtractor(list[fileName]);
                    await sI.setItemForce(
                      fileName,
                      new StoreObject(
                        fileName,
                        'testData',
                        blob
                      ).dataObject
                    )
                  }))
                  _resolve(sI);
                } catch (err) {
                  _reject(err)
                }
              })
            })
            .catch(err => _reject(err))
        })
        .catch(err => _reject(err))
    } else {
      _reject('Invalid URL')
    }
  })
};

I was unable to do the same this what i tried but it never resolves

const test = async (url, id) => {
  if (isValidUrl(url)) {
    try {
      let storage = new Storage(Indexdb, id);
      const cae = new valueExtract(url);
      const data = await cae.fetch();
      return new ZIPExtractor(data)
        .then(obj => obj.getZip())
        .then(obj => obj.getList())
        .then(list => {
          return async (resolve, reject) => {
            try {
              let sI = storage.connection;
              await Promise.all(Object.keys(list).map(async (fileName, index) => {
                let blob = await new FileExtractor(list[fileName]);
                await sI.setItemForce(
                  fileName,
                  new StoreObject(
                    fileName,
                    'testData',
                    blob
                  ).dataObject
                )
              }))
            } catch (err) {
              throw new Error(err)
            }
          }
        })
        .catch(err => _reject(err))
    } catch (e) {
      throw new Error('Invalid URL')
    }
  }
};

Also how do we write test case for these kind of a function so that we need not pass in actual network url and mock in jest.


回答1:


It should fulfill, but with the async (resolve, reject) { … } that you return. You never should've used this in the first place, you can just omit it:

const test = async (url, id) => {
  if (!isValidUrl(url)) {
    throw new Error('Invalid URL')
  }
  const storage = new Storage(Indexdb, id);
  const cae = new valueExtract(url);
  const data = await cae.fetch();
  const obj = await new ZIPExtractor(data); // shudder. A constructor should never return a promise
  const zip = await obj.getZip();
  const list = await zip.getList();
  const sI = storage.connection;
  await Promise.all(Object.keys(list).map(async (fileName, index) => {
    const blob = await new FileExtractor(list[fileName]);
    const store = new StoreObject(fileName, 'testData', blob);
    await sI.setItemForce(fileName, store.dataObject);
  }));
  return sI; // or something?
}


来源:https://stackoverflow.com/questions/59968552/converting-promises-code-to-async-await-and-mocking-test-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!