If I understand the question, the problem is really just about storing the string in a variable, not anything to do with MySQL. If you really want to store the string in a global variable (almost certainly not what you want to do), you can do this:
'use strict';
...
func(mot_rech => {
// Define a propery on the global scope like so:
global.mot_rech = mot_rech;
console.log(`votre mot = ${mot_rech}`);
});
Don't do that. It will make mot_rech available in any module, not just the current file, but only after the callback has executed. You will find yourself cast into a nightmarish world of asynchronous uncertainty and hidden dependencies.
What you might want to do is to assign the value of mot_rech to a variable in the module scope. You can do so like this:
'use strict';
var mot_rech;
...
func(_mot_rech => {
// Assign the value to the variable declared above.
mot_rech = _mot_rech;
console.log(`votre mot = ${mot_rech}`);
});
This is not necessarily a great evil, but it is probably inadvisable. What you probably want to do is to perform some operation, but only after the value of mot_rech has been assigned. So you need some way of notifying the functions that depend on mot_rect that the query has completed and the value is available. The problem is now just about ensuring that a number of asynchronous operations execute in the right order and with the right arguments.
Without any external libraries, Node's events module may be your best bet. You could do something like this:
'use strict';
// Create a new EventEmitter instance. In more complex scenarios, consider
// defining a class that inherits from EventEmitter instead.
const EventEmitter = require('events');
const emitter = new EventEmitter();
...
func(mot_rech => {
// Emit the a 'mot' event and pass the value of mot_rech
emitter.emit('mot', mot_rech);
console.log(`votre mot = ${mot_rech}`);
});
// Listen for the 'mot' event and perform some operation when it happens.
emitter.on('mot', (mot_rech) => {
console.log(`I'm doing something with ${mot_rech}`);
});
// By exporting the emitter, you can listen for the 'mot' event and get the value
// in any file you like.
module.exports = emitter;
Alternatively, you could exploit ES6 Promises, which are available in Node:
'use strict';
...
// Wrap your asynchronous call in a promise.
const promise = new Promise((resolve, reject) => {
func(mot_rech => {
console.log(`votre mot = ${mot_rech}`);
resolve(mot_rech);
});
});
// When the promise resolves, do this.
promise.then(mot_rech => {
console.log(`${mot_rech} est génial!`);
});
// You can export a promise and register a 'then' or 'catch' callback
// in any other module.
module.exports = promise;
If you don't mind relying on external libraries, you might want to consider async or Q. The former makes it much easier to manage asynchronous callbacks; the latter offers a some very useful methods for working with promises. Bluebird, as recommended by @JohnDoe, is another promise library that many people prefer to Q.
Update
I think you are confusing synchronous operations with asynchronous ones. You're query is executed asynchronously; that's just how it is. So you need to use some asynchronous mechanism (e.g. callbacks, events, promises) whenever you want to do something with the value.
For example, if you are using events, make sure that whenever you need to access mot_rech, you always do so from within an event listener, i.e., a function that executes only after some asynchronous operation has completed and emitted an event. Don't simply return the value of mot_rech and expect it to be defined, since functions always return synchronously.
'use strict';
const fs = require('fs');
const emitter = require('events')();
...
// Emit mot_rech once the query has finished
func(mot_rech => {
emitter.emit('mot', mot_rech);
});
// Log mot_rech
emitter.on('mot', mot_rech => {
console.log(mot_rech);
});
// Save mot_rech to a file
emitter.on('mot', mot_rech => {
fs.writeFile('./mot.txt', `My word is ${mot_rech}.`, err => {
console.log(err || 'mot.txt has been saved!');
});
});