admin管理员组文章数量:1025301
I have been going through the JS -> Reason cheatsheet on the Reason ML website. They are very helpful, but none cover the async
/await
syntax available in modern ES.
What is the Reason ML equivalent of this?
import fs from 'mz/fs';
// A cat-like utility
const main = async () => {
if (process.argv.length != 3) {
throw new Error('Expected a file-path');
}
const path = process.argv[2];
const content = await fs.readFile(path);
console.log(content.toString());
};
main().catch(error => console.error(error));
I have been going through the JS -> Reason cheatsheet on the Reason ML website. They are very helpful, but none cover the async
/await
syntax available in modern ES.
What is the Reason ML equivalent of this?
import fs from 'mz/fs';
// A cat-like utility
const main = async () => {
if (process.argv.length != 3) {
throw new Error('Expected a file-path');
}
const path = process.argv[2];
const content = await fs.readFile(path);
console.log(content.toString());
};
main().catch(error => console.error(error));
Share
Improve this question
edited Feb 26, 2018 at 17:11
glennsl
29.2k12 gold badges59 silver badges79 bronze badges
asked Feb 15, 2018 at 15:29
sdgfsdhsdgfsdh
37.3k31 gold badges161 silver badges291 bronze badges
3
- 1 Probably promises? – Icepickle Commented Feb 15, 2018 at 15:33
- There is a language extension called bs-let that would allow you to do this. – Marko Grdinić Commented Apr 19, 2020 at 14:28
- It does not support Windows though, only Mac and Linux. – Marko Grdinić Commented Apr 19, 2020 at 14:49
3 Answers
Reset to default 4ReasonML documentation says:
Note: we might offer a dedicated syntax for JS promises (async/await) in the future.
Which means it doesn't currently support async/await.
There is currently (October 2018) a "Syntax proposal: async/await" Pull Request open to implement this that's been open for about 15 months now. At the end of last year one of the developers wrote a blog post about their plans and noting some of the problems of handling some of the JavaScript Promise quirks. From the blog post there is even an example Github repo with support for async syntax that looks like this:
let getThing = () => Js.Promise.make((~resolve, ~reject) => [@bs]resolve(20));
let getOtherThing = () => Js.Promise.make((~resolve, ~reject) => [@bs]resolve(40));
let module Let_syntax = Reason_async.Promise;
let doSomething = () => {
/* These two will be awaited concurrently (with Promise.all) */
[%await let x = Js.Promise.resolve(10)
and y = getThing()];
[%awaitWrap let z = getOtherThing()];
x + y + z + 3
};
/* Heyy look we have top-level await!
* `consume` means "give me this promise, and have the result
* of this whole expression be ()" */
{
[%consume let result = doSomething()];
Js.log(result)
};
If you like ReasonML but want async functionality, check out OCaml. They have a few syntax differences but are otherwise very similar. Reason even uses OCaml's piler, and is basically OCaml with braces to make Javascript developers less afraid. OCaml has two async libraries in use: Lwt and Jane Street's Async.
I have been going through the JS -> Reason cheatsheet on the Reason ML website. They are very helpful, but none cover the async
/await
syntax available in modern ES.
What is the Reason ML equivalent of this?
import fs from 'mz/fs';
// A cat-like utility
const main = async () => {
if (process.argv.length != 3) {
throw new Error('Expected a file-path');
}
const path = process.argv[2];
const content = await fs.readFile(path);
console.log(content.toString());
};
main().catch(error => console.error(error));
I have been going through the JS -> Reason cheatsheet on the Reason ML website. They are very helpful, but none cover the async
/await
syntax available in modern ES.
What is the Reason ML equivalent of this?
import fs from 'mz/fs';
// A cat-like utility
const main = async () => {
if (process.argv.length != 3) {
throw new Error('Expected a file-path');
}
const path = process.argv[2];
const content = await fs.readFile(path);
console.log(content.toString());
};
main().catch(error => console.error(error));
Share
Improve this question
edited Feb 26, 2018 at 17:11
glennsl
29.2k12 gold badges59 silver badges79 bronze badges
asked Feb 15, 2018 at 15:29
sdgfsdhsdgfsdh
37.3k31 gold badges161 silver badges291 bronze badges
3
- 1 Probably promises? – Icepickle Commented Feb 15, 2018 at 15:33
- There is a language extension called bs-let that would allow you to do this. – Marko Grdinić Commented Apr 19, 2020 at 14:28
- It does not support Windows though, only Mac and Linux. – Marko Grdinić Commented Apr 19, 2020 at 14:49
3 Answers
Reset to default 4ReasonML documentation says:
Note: we might offer a dedicated syntax for JS promises (async/await) in the future.
Which means it doesn't currently support async/await.
There is currently (October 2018) a "Syntax proposal: async/await" Pull Request open to implement this that's been open for about 15 months now. At the end of last year one of the developers wrote a blog post about their plans and noting some of the problems of handling some of the JavaScript Promise quirks. From the blog post there is even an example Github repo with support for async syntax that looks like this:
let getThing = () => Js.Promise.make((~resolve, ~reject) => [@bs]resolve(20));
let getOtherThing = () => Js.Promise.make((~resolve, ~reject) => [@bs]resolve(40));
let module Let_syntax = Reason_async.Promise;
let doSomething = () => {
/* These two will be awaited concurrently (with Promise.all) */
[%await let x = Js.Promise.resolve(10)
and y = getThing()];
[%awaitWrap let z = getOtherThing()];
x + y + z + 3
};
/* Heyy look we have top-level await!
* `consume` means "give me this promise, and have the result
* of this whole expression be ()" */
{
[%consume let result = doSomething()];
Js.log(result)
};
If you like ReasonML but want async functionality, check out OCaml. They have a few syntax differences but are otherwise very similar. Reason even uses OCaml's piler, and is basically OCaml with braces to make Javascript developers less afraid. OCaml has two async libraries in use: Lwt and Jane Street's Async.
本文标签: javascriptDoes ReasonML support asyncawaitStack Overflow
版权声明:本文标题:javascript - Does ReasonML support asyncawait? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560893a2156157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论