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

promise.reject() no params error always undefined #45

Open
huzedong2015 opened this issue Jun 12, 2020 · 2 comments
Open

promise.reject() no params error always undefined #45

huzedong2015 opened this issue Jun 12, 2020 · 2 comments

Comments

@huzedong2015
Copy link

huzedong2015 commented Jun 12, 2020

Demo

function delay(time = 0) {
	return new Promise((reslove, reject) => {
			if (time > 2000) {
				reject();
			} else {
			reslove(time);
		}
	});
};
const [error, data] = await to(delay(2100));
console.log(error, data); // error always undefined

This is just a simple example

Why

There are many UI frameworks. Confirm uses project, and reject does not return parameters

@jxbb
Copy link

jxbb commented Jul 17, 2020

const [error, data] = await to(this.delay(2100),{err: '...'});

@huzedong2015
Copy link
Author

const [错误,数据] =等待(this.delay(2100),{err:'...'});

** await-to-js Source code **

export function to<T, U = Error> (
  promise: Promise<T>,
  errorExt?: object
): Promise<[U | null, T | undefined]> {
  return promise
    .then<[null, T]>((data: T) => [null, data])
    .catch<[U, undefined]>((err: U) => {
      if (errorExt) {
        Object.assign(err, errorExt);
      }

      return [err, undefined];
    });
}

export default to;

Error ext is invalid when reject does not return any data.

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

No branches or pull requests

2 participants