简体   繁体   English

执行 function 的值返回 promise

[英]A value which executes an function returns a promise

I have this piece of code and in the 2nd function where i check the db if i log stuff inside the function it logs fine but if i assign a var and call the function as it returns a true of false i get a pending promise I have this piece of code and in the 2nd function where i check the db if i log stuff inside the function it logs fine but if i assign a var and call the function as it returns a true of false i get a pending promise

const mongoose = require('mongoose')
const config = require('./config.json')
const schema = require('./data')
const mongoactivate = async(config) => {
  await mongoose.connect(config.mongo, {
    useUnifiedTopology: true,
    useNewUrlParser: true
  })
}
mongoactivate(config)
const check = async(p, q) => {
  let data = await schema.findOne({
    username: p,
    pass: q
  })
  let cor = (data === null) ? false : true;
  console.log(cor)
}
const ans = check("p", "q")
console.log(ans)

You are in troubles with async/await.你遇到了异步/等待的麻烦。 You have to await first mongoactivate(config) and check('p', 'q') .您必须先等待mongoactivate(config)check('p', 'q') Try writing await before those functions.尝试在这些函数之前编写await

Good luck my g祝你好运我的g

You're not awaiting the mongoactivate() function.你不是在等待mongoactivate() function。 Async functions always return a Promise.异步函数总是返回 Promise。 Also, your check function never returns anything, so ans won't be the value you'd expect it to be.此外,您的check function 永远不会返回任何内容,因此ans不会是您期望的值。 Return the cor variable at the end of the function.返回 function 末尾的cor变量。

const mongoose = require('mongoose')
const config = require('./config.json')
const schema = require('./data')

const mongoactivate = async (config) => {
  await mongoose.connect(config.mongo, {
    useUnifiedTopology: true,
    useNewUrlParser: true
  })
}

const check = async (p, q) => {
  let data = await schema.findOne({
    username: p,
    pass: q
  })
  let cor = (data === null) ? false : true;
  return cor;
}

(async () => {
  await mongoactivate(config)
  const ans = await check("p", "q")
  console.log(ans)
})()

you need to know how to work with await/async, JS is not like C# or C if you still have some issues with that i advice you to resolve the promise like in this exemple:你需要知道如何使用等待/异步,JS 不像 C# 或 C 如果你仍然有一些问题,我建议你解决 promise 像这个例子:

 const Afunction = async(p,q)=> {
 let ThePromise = new Promise((resolve,reject) => {
      //resolve the pending promise that you got
         let data = await schema.findOne({
         username: p,
         pass: q
         })
       let cor = (data === null) ? false : true; 
       resolve(cor)
 })
return ThePromise.then(val=> {return val} )
}

And then you can get the result with:然后你可以得到结果:

  const TheValueIwant = await Afunction ("p","q");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何包装返回承诺的延迟函数,以便外部函数返回实际值而不是承诺 - How to wrap deferred function which returns promise so outer function returns real value not a promise Function 返回一个回调或 promise - Function which returns a callback or a promise JS异步(异步/等待)返回promise但也执行该功能 - JS async (async/await) returns promise but also executes the function 返回承诺未按预期工作的函数 - Function which returns a promise not working as expected 如何在返回新诺言的函数中使用另一个诺言? - How to use another promise in a function which returns a new promise? 未捕获(在承诺中):如何等待在循环内返回承诺的函数? - Uncaught (in promise):How to await a function which returns a promise inside a loop? 返回 Promise 的函数,这是 Promise 解决后要使用的一组选项 - Function that returns a promise, which is the set of options to be used once the promise resolves Javascript异步函数返回承诺而不是实际值 - Javascript Async function returns promise and not actual value 处理返回承诺或空值的函数 - handle a function that returns a promise or a null value 异步 function 返回 promise。如何使 promise 值全局化? - Async function returns a promise.How to make the promise value global?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM