I need help figuring out how to make this recursive function return a promise value. I've attempted various approaches, but they all resulted in the search
variable ending up as undefined
.
public search(message: Message) {
let searchResult: string;
const filter = (msg: Message) => msg.author.id === message.author.id;
message.channel.send('Please enter a search term').then(msg => {
message.channel.awaitMessages(filter, { max: 1 })
.then(collected => {
if (collected.first()!.content === 'Test') this.search(message);
msg.delete()
collected.first()!.delete()
searchResult = collected.first()!.content
})
})
})
return searchResult; // Error: Variable 'search' is used before being assigned.ts(2454)
}