When I add the new JavaScript keyword async to my code, Dreamweaver says there is a syntax error and 'code hinting may not work until you fix this error'.
Q: How do I add async to the list of approved keywords such that:
async function myFunction() {}
does not produce a syntax error in Dreamweaver?
Related
I am going through some code on github: https://github.com/linnovate/mean/blob/master/server/routes/user.route.js
But there is a portion of it I don't understand, that is:
router.route('/')
.post(asyncHandler(insert));
On npm express-async-handler
is described as:
Simple middleware for handling exceptions inside of async express routes and passing them to your express error handlers.
They go give an example of how to use the module, but it doesn't explain much.
So my questions are:
How is the insert function on line 12 called without parentheses?
What is the function of asyncHandler(), what would the code look like if you decide on not using it?
Normally when using router.route('/').post there are curly braceswhich follow. In this code I cant see any. So my question are: Is the async function insert part of the function body of router.route('/').post? and if not then why are there no curly braces?
What exactly is being exported here user.controller.js on line 14 (is it an object, a var...)? What is the advantage of exporting it this way? Why not just export the function insert()?
Thank you in advance.
How is the insert function on line 12 called without parentheses?
The insert function is not called here. It is passed to asyncHandler() as a function reference so it can be called later. It is asyncHandler() which is called immediately and that returns a new function that is passed to .post() as the request handler.
What is the function of asyncHandler(), what would the code look like if you decide on not using it?
This is a wrapper around insert that looks for a rejected promise returned from the function and, if found, calls next(err) automatically.
Normally when using router.route('/').post there are curly braceswhich follow. In this code I cant see any. So my question are: Is the async function insert part of the function body of router.route('/').post? and if not then why are there no curly braces?
I'm not sure what you mean by curly braces. .post() expects a function reference to be passed to it that will be called with a certain set of parameters when the defined route matches an incoming request. It can use as either of these:
// externally defined request handler function
router.route('/').post(someRequestHandlerFunc);
// inline defined request handler function
router.route('/').post(function(req, res, next) {
// code here for the request handler
});
What exactly is being exported here user.controller.js on line 14 (is it an object, a var...)? What is the advantage of exporting it this way? Why not just export the function insert()?
I'm assuming the line 14, you're asking about is here. That's just exporting the insert function on the insert property of this modules exports. When you export a function, you don't use insert(). That calls the function immediately. You just refer to the function's name as insert to export a reference to the function that can be called later.
The reason to export is as a property of an object rather than just export the single function is to make the module extensible so it can export other things as different named properties.
I use ESLint to lint my code and I want to implement the stream.Writable. I can successfully run my code but ESLint report an error message:
[eslint] Expected 'this' to be used by class method '_write'. (class-methods-use-this)
I try to find the solution on the ESLint page. It says I can make the _write method static. But it causes an error when I add the static in front of the _write.
Error: _write() is not implemented
Here is my code. Are there have any way to fix it? Or I should make an exception of ESLint?
class MyWritable extends stream.Writable {
_write(chunk, encoding, next) {
console.log(chunk.toString());
next();
}
}
If you transform your method to a static one, you need to call it from the class and not the instance, like this: MyWritable._write(...).
If you are currently developing your method and at term it will use this you can disable the rule for the method or the file. You can also completely disable the rule if you feel it's not helping you write better code.
I use Visual Studio Code and #types/node (7.0.8) but it seems that some functions etc. have a wrong formatted code comment and therefore Visual Studio Code and Visual Studio 2017 won't show any quickinfos in IntelliSense.
example
import * as fs from 'fs';
fs.unlink
When I type fs.unlink VS Code display the function signature but not the comment whitch is defined in
./node_modules/#types/node/index.d.ts
on line 2400
/*
* Asynchronous unlink - deletes the file specified in {path}
*
* #param path
* #param callback No arguments other than a possible exception are given to the completion callback.
*/
export function unlink(path: string | Buffer, callback?: (err?: NodeJS.ErrnoException) => void): void;
The culprit here is the first line which misses an asterisk.
The correct notation would be
/**
As soon as I change index.d.ts like that, I get working IntelliSense.
Some functions are commented corretly while others are not.
Am I doing something wrong here? (are those functions not meant to be used despite being exported)
Is it an error in #types/node and if so are there ways to teach VS Code to parse those comments?
Thanks
I work on TS and JS support for VSCode. The use of /* instead of /** looks like a bug in the node d.ts type definition. As far as I know, there is no way to configure TypeScript or VSCode to treat plain old /* comments as documentation comments.
I've submitted a PR with the fix: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15285
Can someone give me some elementary tips about globals.js in sails.js, I have below questions.
I commented async in globals.js, I have removed async module which was installed in my project, now in a service I commented line //var async = require('async'); My code still works where ever I used async, I am new to sails and exploring the use of globals.js, is that like if async is commented any usage should throw an error "undefined".
How to add my own module to globals.js?
How is bootsrap.js different from global.js?
Appreciate your discussions and explanation.
Thanks
1) async is module that exposed to global by Sails. Here is the code that responsible for it (full code here - https://github.com/balderdashy/sails/blob/a210b48667708bea687dd5ec61ce9e07ffc0c005/lib/app/private/exposeGlobals.js#L35)
if (sails.config.globals.async !== false) {
global['async'] = async;
}
You need to set async to false in config/globals.js and then you can't call async.
2) It's not a frameworks' question. You just make global['myModule'] = myModule but don't do this, please.
3) bootstrap.js is a function that calls before sails initialisation.
Okay so async is by default commented in globals.js and is available globally. When I uncommented that and set "async: false" I got the error I expected.
error: Error (E_UNKNOWN) :: Encountered an unexpected error
ReferenceError: async is not defined
I have a massively repeating code pattern like this:
public SomeReturnType InitiateAndSecureTransaction(string token, IPaymentDetailsUpdateable paymentDetails)
{
return WithService<MyServiceClient, SomeReturnType>(proxy => proxy.InitiateAndSecureTransaction(token, paymentDetails));
}
To help me implement this pattern in some fifty odd methods, I have a live template like this:
return WithService<$CLASS_NAME$ServiceClient, $RETURN_TYPE$>(proxy => proxy.$CONTAINING_METHOD$($PARAMS$));
The $PARAMS$ parameter is assigned the macro Execute smart completion, and this works wonderfully, but I'm spoilt. I insert the whole WithService call with about three tabs, but ReSharper only gets the first parameter of the enclosing method, and I have to intellisense through the rest. Is there any way I can simply copy all of the enclosing method's paramaters?