Repository navigation
Can i use as not-module #150
|
Hello, how you all doing? I'm new to node so this might be a stupid question. I'm trying to use this library to make a sort of bot which will send me my daily homework and/or what was done during the lesson. I re-use the code in the example, making me have to "compile?" my code as a "module" (to use import { ... } from .. and await, which i'm not sure i understand the use of either but the code won't work without it). I went around this "module" issue by using import ... from for every library i need. But now when i try to put my code in a function (to use it in loops or scheduling execution ....) it just stop working when it's as a function. Please help!!! (don't hesitate to ask for any more info or bits of code) |
Replies: 1 comment 8 replies
EditPardon, j'avais mal compris. async function thisIsAnAsyncFunction() {
// Here, you can wait for promises with 'await'
}Tu n'as pas besoin de convertir ton projet vers les modules, même si c'est plus pratique. Si ça ne marche pas dans les fonctions, c'est peut-être parce que tu n'as pas utilisé (async () => {
// Here is the code; it's actually defining an async function and then executing it.
// If you use this, don't forget to declare your imports outside, at the top of the file
})()N'hésite pas à me partager ton code, je t'aiderais avec plaisir! Réponse originaleSalut ! Tu peux parler français ici, le reste est juste en anglais par convention. ;) La version courte :
Pour rappel, les imports se font en haut des fichiers. Ca peut se faire comme ça : const Ed = require("ecoledirecte.js")Ou, sur les versions plus récentes ou TS, comme ça : import * as Ed from "ecoledirecte.js"
const session = new Ed.Session("identifiant", "motdepasse");
// etc...Tu as parlé d' Dis-moi si t'as compris! Et n'hésite pas à laisser une étoile sur le repo s'il t'est utile :) |
Edit
Pardon, j'avais mal compris.
awaitn'est normalement pas disponible au premier niveau, il faut l'utiliser dans une fonction asynchrone:Tu n'as pas besoin de convertir ton projet vers les modules, même si c'est plus pratique. Si ça ne marche pas dans les fonctions, c'est peut-être parce que tu n'as pas utilisé
async. Si tu veux revenir àCommonJS(avec require et sans les await de premier niveau), tu peux le faire. En général, on fait ça pour utiliser await en CommonJS au premier niveau: