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

Why return function ref ? #855

Open
indown opened this issue Dec 6, 2021 · 6 comments
Open

Why return function ref ? #855

indown opened this issue Dec 6, 2021 · 6 comments

Comments

@indown
Copy link

indown commented Dec 6, 2021

Now, we need to start altering our "then" methods so that they return promises for the return value of their given callback. The "ref" case is simple. We'll coerce the return value of the callback to a promise and return that immediately.

var ref = function (value) {
    if (value && typeof value.then === "function")
        return value;
    return {
        then: function (callback) {
            return ref(callback(value));
        }
    };
};

I try only to use "return callback(value)", It doesn't seem to be a problem,Can you provide an example (" return callback(value) ")?
Thanks.

@wmertens
Copy link
Collaborator

wmertens commented Dec 6, 2021

Could you elaborate? It's not clear to me what you are trying to achieve and how Q is involved in it

@indown
Copy link
Author

indown commented Dec 6, 2021

Could you elaborate? It's not clear to me what you are trying to achieve and how Q is involved in it

design/README.md

That's my problem with this document design/README.md.

@wmertens
Copy link
Collaborator

wmertens commented Dec 6, 2021

If you don't call ref on the callback value, the callback might not return a promise and then you wouldn't be able to call p.then(...).then(...)

@indown
Copy link
Author

indown commented Dec 6, 2021

If you don't call ref on the callback value, the callback might not return a promise and then you wouldn't be able to call p.then(...).then(...)

I try only to use "callback(value)". it would be able to call p.then(...).then(...)
https://jsbin.com/cozuluxido/3/edit?html,js,console,output
Can you provide an example where it doesn't work?

@kriskowal
Copy link
Owner

p.then(() => 1) would return 1, would it not?

Of course, there is a school of thought that promises should be strict monads, in which case, the author of the callback would be obliged to wrap/lift the return value in order to satisfy the type of then (which would decompose into map and flatMap).

So p.then(() => ref(1)) would return a promise.

@indown
Copy link
Author

indown commented Dec 7, 2021

p.then(() => 1) would return 1, would it not?

Of course, there is a school of thought that promises should be strict monads, in which case, the author of the callback would be obliged to wrap/lift the return value in order to satisfy the type of then (which would decompose into map and flatMap).

So p.then(() => ref(1)) would return a promise.

promise: {
            then: function (_callback) {
                let result = defer();
                let callback = function (value) {
                    result.resolve(_callback(value));
                };
                if (pending) {
                    pending.push(callback);
                } else {
                    value.then(callback);
                }
                return result.promise;
            }
        }

So p.then(() => 1) would return a promise.
There is an example on https://jsbin.com/cozuluxido/3/edit?html,js,console,output

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

3 participants